简体   繁体   English

JavaScript-如何仅运行setInterval()1次

[英]JavaScript - how to run setInterval() only 1 time

<button onClick="increase = setInterval(increasing, 400);
        clearInterval(decrease);"><b>Sicaklik Arttir (+)</b>
</button>

<button onClick="clearInterval(increase);
        clearInterval(decrease);"><b>Stop</b
</button>

<button onClick="decrease = setInterval(decreasing, 400);
        clearInterval(increase)"><b>Sicaklik Azalt  (-)</b>
</button>

I have buttons that include setInterval . 我有包含setInterval按钮。 When I press button, setInterval is called, but when I press again, setInterval is called again, and I can't stop it. 当我按下按钮时, setInterval被调用,但是当我再次按下时, setInterval被再次调用,而我无法停止它。

How do I stop it? 如何停止?

I want to run setInterval only one time. 我只想运行setInterval一次。

There is setTimeout method which calls only once at a time. 有setTimeout方法,一次仅调用一次。

setTimeout(increasing, 400)

To clear the timeout use clearTimeout method. 要清除超时,请使用clearTimeout方法。

Check the return value before button clicked 2nd time and before timeout of 400 seconds , 在第二次单击按钮之前和超时400秒之前检查返回值,

 if(! increase)
   increase = setTimeout(increasing, 400);

Then clear timeout : 然后清除超时:

 clearTimeout(increase);

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

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