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 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行