javascript - 计时器计数不正确

标签 javascript jquery

http://jsfiddle.net/zAFND/640/

在 jsfiddle 中,我遇到的问题是响应时间。它增加了 2 秒而不是 1 秒。我相信这个问题是因为我有两个问题,所以它会计算每个响应时间文本输入中两个问题的秒数,因此每次都会增加 2 秒。但我的问题是如何解决这个问题,以便计时器正常计数:

响应时间代码:

` var 响应 = "00:00:00", 响应部分 = 响应.split(':'), 响应时间 = +responseparts[0], 响应分钟 = +响应部分[1], 响应秒 = +responseparts[2];

     function correctResponse(responsenum) {
         return (responsenum < 10) ? ("0" + responsenum) : responsenum;
     }

 var responsetimer = [];
 $('.queWrap').each(function(index, element) {
 var wrap=$(this),   
 input = wrap.find('.responseTime'),
 checkbox=wrap.find('#ck-button').find('input'),
 clickInput=wrap.find('.mouseClick');


     responsetimer[index] = setInterval(function () {
     responseseconds++;
     if (responseseconds == 60) {
         responseseconds = 00;
         responseminutes++;

         if (responseminutes == 60) {
             responseminutes = 00;
             responsehours++;

             if (responsehours <= 24) {
                 clearInterval(responsetimer);
                 return;
             }

         }
     }
     input.val(correctResponse(responsehours) + ":" + correctResponse(responseminutes) + ":" + correctResponse(responseseconds));
 }, 1000);

`

最佳答案

移动 var 声明

response = "00:00:00",
responseparts = response.split(':'),
responsehours = +responseparts[0],
responseminutes = +responseparts[1],
responseseconds = +responseparts[2];

在每个回调中

http://jsfiddle.net/h8srH/1/

此外,将您的 ck-button id 更改为类。 Id 应该是唯一的。

关于javascript - 计时器计数不正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14794647/

相关文章:

javascript - Angular 服务/工厂配置数据

javascript - 将数组添加到哈希表

jquery - 一个函数中有两件事,其中之一必须等待

JQuery:删除第 20 个以上项目

asp.net-mvc-4 - Jquery Ajax 调用 - 传递多个参数避免名称依赖

jQuery 添加 http ://as prefix to text input if there is none

javascript - 如果输入的值在angularJS中不为空,如何更改输入的CSS

javascript - 将 js 文件替换为相应的缩小版本

javascript - 我的用户可以通过单击缺少 Ajax 的按钮来访问 $_SESSION 的内容吗?

javascript - 如何在单击元素时撤销外部功能?