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

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

报告相同问题?

悬赏问题

  • ¥15 mmocr的训练错误,结果全为0
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀