gpt4 book ai didi

javascript - setTimeout加倍

转载 作者:行者123 更新时间:2023-12-02 17:52:09 24 4
gpt4 key购买 nike

我有两个带有开始和停止按钮的 div,我用作计时器。我创建了一个 json 对象来存储一些时间变量和其他一些东西。当我点击开始时,计时器开始工作。我将对象的属性之一设置为等于超时函数 current_job = jobs[current_job_id];,然后 current_job.t = setInterval(function(){...

当我单击其他按钮之一时,两个 setIntervals 似乎加倍了。我究竟做错了什么?演示在这里: http://codepen.io/EightArmsHQ/pen/2d60722bb802bd1c3da5fe42cbc15442

下面的大部分代码:

var jobs = [];

$(".job").each(function(i){
jobs[i] = {
"start_time" : false,
"stop_time" : false,
"job_number" : $(this).find("h1").text(),
"time" : 0,
"chunk_id" : false,
"t" : false,
}
$(this).attr('id', i);
})

$(".start_timer").click(function(){

current_job_div = $(this).closest(".job");
current_job_id = parseInt(current_job_div.attr("id"));
current_job = jobs[current_job_id];


current_job.t = setInterval(function(){
current_job.time += 1;
current_job_div.find(".time_spent").text(make_time(current_job.time));
},1000);
});

$(".stop_timer").click(function(){

current_job_div = $(this).closest(".job");
current_job_id = parseInt(current_job_div.attr("id"));
current_job = jobs[current_job_id];

clearInterval(current_job.t);
});

最佳答案

关闭关闭关闭,请参见此处:http://ejohn.org/apps/learn/#48

代码的工作示例:http://codepen.io/anon/pen/hkEga

(编辑:^虽然你确实有一个错误 - 尝试多次按开始:))

关键代码更改:

current_job.t = (function (current_job, current_job_div) {
return setInterval(function(){
current_job.time += 1;
current_job_div.find(".time_spent").text(make_time(current_job.time));
},1000);
}(current_job, current_job_div));

关于javascript - setTimeout加倍,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21255573/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com