简体   繁体   English

设置超时和清除超时

[英]Settimeout and ClearTimeout

i need to restart a setTimeout after stopping it with a clearTimeout the code is like:我需要在使用clearTimeout停止setTimeout后重新启动它,代码如下:

function start(){timeout = setTimeout(function(){...}, 1000}
function increment(){interval();}
function stop(){clearTimeout(timeout)}

better explained: function start() is a timeout of 1000ms for another function that recall start() .更好的解释:对于另一个调用start()函数, function start()是 1000 毫秒的超时。 function increment() just add +1 to a value every 1000s but it doesn't matter with the problem. function increment()只是每 1000 秒将 +1 添加到一个值,但这与问题无关。 the last function stop() stops the setTimeout in the function start() .最后一个function stop()停止function start()setTimeout i need to stop the setTimeout in start() for just 1000ms and then let it continue working.我需要在start()停止setTimeout仅 1000 毫秒,然后让它继续工作。

ok.... i "avoid" the problem.... i need to stop the setTimeout for 1000ms and then let it works normally... i tried so... instead of pausing the setTimeout I put another time var.好的......我“避免”这个问题......我需要停止 setTimeout 1000ms 然后让它正常工作......我试过了......而不是暂停 setTimeout 我把另一个时间变量。

 var time = 1000;
    var timecache = 1000;
    fatica = 3000;
    sudore = 3000;
    function interval() { timeout = setTimeout(increment, time);} //here setTimeout uses var time//
    function increment(){console.log(time);point += 1;document.getElementById("pul").value = point;interval();}
    function fermafatica() {time = timecache;setInterval(ferma, fatica);} //here the function equals time to timecache so vas time is always 1000ms//
    function ferma(){time = 10000; setTimeout(fermafatica, sudore);} // then here I transformed the time in 10000 so the first function will take 10000 instead of 1000 in setTimeout//
//plus i put a setTimeout to recall function fermafatica() that reset the time to 1000//

this i what i want... I avoid the problem and found another way to do that... but it works... thank you anyway这就是我想要的......我避免了这个问题并找到了另一种方法来做到这一点......但它有效......无论如何谢谢你

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM