setInterval和setTimeout [英] setInterval and setTimeout

查看:94
本文介绍了setInterval和setTimeout的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

var myTimer = setInterval(function(){
    var eleID = '';
    var delayTimer = '';
    $('#hp-fcas li').each(function(i) {
        eleID = $(this).attr('id');
        delayedTrigger( $('#'+eleID + ' a'), 7000*i);
    });
    function delayedTrigger(elem, delay){
        setTimeout(function(){
            $(elem).trigger('click');
        }, delay );
    }
}, 21000);

$(".play").click(function(){
    clearTimeout();
    clearInterval(myTimer);
});

第一次间隔是21秒,

第二个间隔是3个7秒实例(我想要的).

the 2nd interval is 3 7-second instances (what I want).

当我单击.play时,我试图清除以上所有内容.

I'm trying to clear all of the above when I click .play.

有帮助吗?

推荐答案

clearTimeout需要传递一个超时ID,该ID由setTimeout返回.

clearTimeout needs to be passed a timeout ID, this ID is returned by setTimeout.

clearTimeout();不执行任何操作.您可以将返回值从setTimeout推入数组,然后遍历它们,然后运行clearTimeout.

clearTimeout(); does nothing. You can push the return values from setTimeout into an array, and then loop through them, and run clearTimeout.

var timeouts = []; // Array of timeouts
var myTimer = setInterval(function(){
    var eleID = '';
    var delayTimer = '';
    $('#hp-fcas li').each(function(i) {
        eleID = $(this).attr('id');
        delayedTrigger( $('#'+eleID + ' a'), 7000*i);
    });
    function delayedTrigger(elem, delay){
       timeouts.push(setTimeout(function(){ // push onto array
           $(elem).trigger('click');
       }, delay));
    }
}, 21000);

$(".play").click(function(){
    $.each(timeouts, function(i,v){ // clear all timeouts
       clearTimeout(v);
    });
    clearInterval(myTimer);
});

这篇关于setInterval和setTimeout的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆