如何只运行一次setInterval()

JavaScript - how to run setInterval() only 1 time

本文关键字:一次 setInterval 运行      更新时间:2023-09-26
<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>

我有按钮,包括setInterval。当我按下按钮时,setInterval被调用,但当我再次按下按钮时,setInterval又被调用,并且我无法停止。

我该如何阻止它?

我只想运行setInterval一次。

setTimeout方法一次只调用一次。

setTimeout(increasing, 400)

使用clearTimeout方法清除超时

检查按钮第二次点击前和400秒超时前的返回值,

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

然后清除timeout:

 clearTimeout(increase);