我想在点击按钮3秒后执行一段代码。我尝试过使用setInterval
和setTimeout
但是通过观察它,我发现它们重复执行代码但我只想要一次。
function stoptimer(data) {
$('.stop-btn').each(function () {
var wait = $(this).data('key');
if (timerData[wait]['timerId'] != null) {
clearInterval(timerData[wait]['timerId']);
if (timerData[data]['timerId'] != timerData[wait]['timerId']) {
setTimeout(function () {
timer(wait, timerData[wait]['remaining'], global_interval, global_timer_interval)
}, 3000);
//^^ I want to execute this timeout only once not at every 3 seconds.
}
}
});
timerData[data]['timerId'] = null;
}
答案 0 :(得分:0)
也许您需要从
更改代码setTimeout(function () {
timer(wait, timerData[wait]['remaining'], global_interval, global_timer_interval)
}, 3000);
到
timerData[wait]['timerId']=setInterval(function () {
timer(wait, timerData[wait]['remaining'], global_interval, global_timer_interval)
}, 3000);