【问题标题】:How to stop a setTimeout for a decided time如何在确定的时间内停止 setTimeout
【发布时间】:2016-06-21 14:32:05
【问题描述】:
var time = 1000;
var point = 0;
function interval() { timeout = setTimeout(increment, time);}
function increment(){point += 1;document.getElementById("pul").value = point;interval();}
time2 = 3000;
function fermafatica() {setInterval(ferma, time2);}
function ferma(){clearTimeout(timeout)}

我需要停止 interval() 函数或 interval() 中的 setTimeout 仅 1000 毫秒,然后让它继续工作

【问题讨论】:

  • “撤消 clearTimeout”是什么意思?阻止它运行?您需要展示您如何使用xyz
  • 你的问题有点混乱。你是什​​么意思撤消cleartimeout,你的意思是运行clearTimeout?
  • 你在哪里存储超时(我知道它在技术上是全局的,但那非常糟糕)。不清楚你在问什么,因为 clearTimeout 不是超时,所以如果你想在超时时运行函数 z 使用... setTimeout。
  • 我已经编辑了我的答案。请看看这是否能解决您的问题。
  • 不是我要搜索的内容,但可能有解决方案……在那儿

标签: javascript time set settimeout cleartimeout


【解决方案1】:

您是否询问如何在 X 秒后清除超时?我会考虑在重复函数中只计算一个静态变量(我们称之为 X),如果函数每 10 毫秒重复一次,并且您希望它运行 1000 毫秒,那么当 x==100 时,清除超时。

【讨论】:

  • 那里有代码,使用 clearTimeout 函数。
  • 你不懂...我需要在1000ms后停止clearTimeout...
  • 我想我明白了。为什么没有一个布尔值,如果为真,则阻止循环函数运行。像 if(bool==true){ //do the normal stuff} else{bool=true} 之类的东西,然后在您希望它不运行 1000 毫秒时将 bool 设置为 false。这将使它跳过一个超时周期,这似乎是你想要的
  • 试过....如果我设置 Bool=true 并设置函数 ferma(){clearTimeout(timeout); Bool=false} 代码读取它就像它是真的......
【解决方案2】:

你可以试试:

setTimeout(function() {
    z();
    // Do anything else here
}, 1000);

setTimeout 函数在设定的时间后运行给定的函数。这将在提供的1000ms 时间之后调用z() 函数,该函数清除您的超时。

编辑:一切:见下文

var time = 1000;
var point = 0;
function interval() { timeout = setTimeout(increment, time);}
function increment(){point += 1;document.getElementById("pul").value = point;interval();}
time2 = 3000;
function fermafatica() {setInterval(ferma, time2);}
function ferma(){clearTimeout(timeout)}

更改function fermafatica() {setInterval(ferma, time);}

function fermafatica() {setTimeout(ferma, time);}

【讨论】:

  • setTimout 搞砸了所有代码...因为在某个时间段内增加值代码...如果我使用另一个 setTimeout 增加开始“跳跃”并停止正常...不知道为什么。 .
  • 您的完整代码与缩短的代码不同。您有setInterval(ferma,这与您的第一个示例中的setInterval(x 不同。我仍然不知道您所说的is n increasing value code 是什么意思。 n 是什么?
  • 是的,名称已更改,但它调用的实际函数与您的示例不一致。例如。在您的真实示例中,您调用的相当于setInterval(z
【解决方案3】:

好的....我“避免”了这个问题....我需要停止 setTimeout 1000 毫秒,然后让它正常工作...我尝试过...而不是暂停 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//

这就是我想要的……我避免了这个问题,并找到了另一种方法来做到这一点……但它确实有效……

【讨论】:

  • jsfiddle.net/th4p68h3/1 如果你想要完整的代码和html(这是一个愚蠢的游戏......在preprepreprealpha中......尝试理解......)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-02-28
  • 1970-01-01
  • 2011-02-25
  • 2019-10-01
  • 2012-01-16
相关资源
最近更新 更多