dongxiang3205 2015-06-13 05:26
浏览 83
已采纳

重置按钮并启动倒数计时器

I am trying to begin a countdown timer when a button is clicked. I have searched and found a few different items, but nothing that seems to function.

I have different buttons throughout the domain that will set different times (IE: 120 seconds, 60 seconds, etc). The display is in the same spot on every page ( div in the top right of the loaded page ) so I was wanting to use an external js scripts/timer.js

I can use <div id="timer"></div> to place the timer.

My questions: How do I call the function to start the timer. What is a good suggested script for said timer when the amount of time can change and it has to carry over pages (I've been using $_SESSION['x']to pass information between .php

<div class="headertopgab">
    <div style="float: left; margin-left: 20px">
        Galactic Credits: 
        <?php echo "&Ccedil;".$_SESSION['galacticCredits']; ?>
    </div>
    <div style="margin-left: 50px; float: left">
        <?php echo "".$_SESSION['currentLocation']; ?>
    </div>
    <div style="float:right">
        <div id="time">10</div>  <!-- this is where I want the countdown-->
    </div>
</div>
  • 写回答

1条回答 默认 最新

  • dongqiao6730 2015-06-13 05:38
    关注

    you can use this

    var countdown;
    var counter = function(){
        var time = parseInt($('#time').text());
        if (time !== 0){
            $('#time').text(time - 1);
        }else{
            clearInterval(countdown);
        }
    }
    countdown = setInterval(counter , 1000);
    

    DEMO

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 STM32 INMP441无法读取数据
  • ¥100 求汇川机器人IRCB300控制器和示教器同版本升级固件文件升级包
  • ¥15 用visualstudio2022创建vue项目后无法启动
  • ¥15 x趋于0时tanx-sinx极限可以拆开算吗
  • ¥500 把面具戴到人脸上,请大家贡献智慧
  • ¥15 任意一个散点图自己下载其js脚本文件并做成独立的案例页面,不要作在线的,要离线状态。
  • ¥15 各位 帮我看看如何写代码,打出来的图形要和如下图呈现的一样,急
  • ¥30 c#打开word开启修订并实时显示批注
  • ¥15 如何解决ldsc的这条报错/index error
  • ¥15 VS2022+WDK驱动开发环境