setTimeout或setInterval,3秒后超过POC一次不再重复

时间:2016-05-26 05:34:29

标签: javascript jquery timer

我想在点击按钮3秒后执行一段代码。我尝试过使用setIntervalsetTimeout但是通过观察它,我发现它们重复执行代码但我只想要一次。

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;
}

1 个答案:

答案 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);