jquery函数和变量setInterval循环的循环次数?
我有代码(这是工作),但我需要: 1.单击(“.my div”)后,功能应停止 2.之后,该功能应在可变时间段(可能是10秒、15秒、5秒、30秒)后启动 代码始终有效,我不知道如何修复它。您可以使用该函数,然后创建一个新的间隔,以便在使用一段时间后重复它jquery函数和变量setInterval循环的循环次数?,jquery,settimeout,Jquery,Settimeout,我有代码(这是工作),但我需要: 1.单击(“.my div”)后,功能应停止 2.之后,该功能应在可变时间段(可能是10秒、15秒、5秒、30秒)后启动 代码始终有效,我不知道如何修复它。您可以使用该函数,然后创建一个新的间隔,以便在使用一段时间后重复它 我不确定你想要实现什么,但你可以尝试以下逻辑: var interval = null; // the function to be called each second function x(){ console.log('run
我不确定你想要实现什么,但你可以尝试以下逻辑:
var interval = null;
// the function to be called each second
function x(){
console.log('running');
}
//start interval
function startInterval(){
interval = setInterval(x, 1000);
}
// called at (".my div") click
// stop the interval, then restart it after 10 seconds
// save the timeout so it's not called more than once
var restartTimeout = null;
function interruptInterval(){
if(interval && !restartTimeout){
clearInterval(interval);
restartTimeout = setTimeout(startInterval, 10000);
}
}
$(".my div").click(interruptInterval);
startInterval();
听起来有点模糊,你想让它停止,但它需要重复?在按下div后,我的函数应该停止,并在可变时间段(可能是10秒、15秒、5秒、30秒)后启动。我很难理解代码背后的逻辑,尤其是
interval
variable的用法我在这里有点迷茫。因此,您正在创建一个间隔,该间隔使用时间计数器调用myFunction
。然后启动另一个间隔调用anim()
,它使用第一个间隔的间隔ID作为时间?为什么?我不知道如何使用几个间隔。可能是个例子吗?谢谢,这是我需要的对不起,我不能增加排名(我是新来的,没有足够的投票排名)!再次感谢你!
var interval = setInterval(myFunction, counter);
//after clicking the .my div
clearInterval(interval);
var second_interval = setInterval(myFunction, counter);//Maybe an other function than myFunction?
var interval = null;
// the function to be called each second
function x(){
console.log('running');
}
//start interval
function startInterval(){
interval = setInterval(x, 1000);
}
// called at (".my div") click
// stop the interval, then restart it after 10 seconds
// save the timeout so it's not called more than once
var restartTimeout = null;
function interruptInterval(){
if(interval && !restartTimeout){
clearInterval(interval);
restartTimeout = setTimeout(startInterval, 10000);
}
}
$(".my div").click(interruptInterval);
startInterval();