gpt4 book ai didi

Javascript 检查一段时间内是否有事情没有发生

转载 作者:行者123 更新时间:2023-12-03 07:32:57 25 4
gpt4 key购买 nike

var myVar = uncontrollableNumber;
$("#myButton").click(function(){

myVar++;

});

有没有一种方法可以检查“如果 myVar 在 20 秒内没有改变(可以向上或向下,代码只是一个示例),则执行某些操作,如果确实改变,则重新开始倒计时”?我尝试了一些代码但失败了。

function myFunction(myVar){
var oldMyVar = myVar;
while(oldMyVar === myVar){
var countdownDone = false;
//countdown then run function
countdownDone = true;
}
if(!countdownDone){
myFunction();
}

最佳答案

这是一个working JS fiddle为你。

还添加代码片段

var myVar = 0;
var oldVar = 0;
$("#myButton").click(function(){
myVar++;
});

setInterval(function(){
checkForChanges(); // after every 2 secs check
oldVar = myVar; // reset the old value now to the new one, so during next check we should have our myVar changed.
},2000);

function checkForChanges(){
if(oldVar === myVar){
$('#response').append('same value nothing has changed!!'+'<br/>');
//alert('same value nothing has changed!!');
}
else{
$('#response').append('The value changed!!'+'<br/>');
//alert('The value changed!!');
}
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button id="myButton">
click Me For Change!!
</button>

<div id="response">

</div>

关于Javascript 检查一段时间内是否有事情没有发生,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35740674/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com