admin管理员组文章数量:1318564
I have a JS function which gets called when a button is clicked. The same function gets called after a timeout too (using setTimeout). How can I avoid a race condition when somebody clicks the button and setTimout gets called at the same time ?
I have a JS function which gets called when a button is clicked. The same function gets called after a timeout too (using setTimeout). How can I avoid a race condition when somebody clicks the button and setTimout gets called at the same time ?
Share Improve this question asked Jun 27, 2018 at 4:07 sharathsharath 3,6269 gold badges51 silver badges78 bronze badges 2- 1 Use some sort of flag to detect if its altered before, tracking previous function calls – user2575725 Commented Jun 27, 2018 at 4:11
- you'll have to provide some code to demonstrate the potential issue you've imagined – Jaromanda X Commented Jun 27, 2018 at 4:12
4 Answers
Reset to default 6You seem to be thinking that these two functions might be called at exactly the same time so it will cause a race condition. But Javascript uses a single threaded Event Loop which means these functions will pushed to a stack of functions that would be executed one by one no matter when they are called.
The JS function that gets called when a button is clicked is a callback function. The JS function that gets executed after a timeout is also a callback function. In your case, these 2 just happen to be the same. Both of them will be placed on the task queue from which the JS event loop will move them to the call stack when the call stack is clear. When each one of them is placed in the call stack, they gets executed. So after the first one is placed on the queue, it gets executed. Then the call stack is clear. Then the JS event loop pulls the second from the task queue and place on the call stack.
The JS runtime can only do one thing at a time. So there's no race condition.
This video https://www.youtube./watch?v=8aGhZQkoFbQ can describe in more details
You can use 'setInterval' instead of 'setTimeout'. So in the button click you can clear the interval and set the interval again. You can refer the code below.
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis./ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script>
var myVar;
$(document).ready(function(){
myVar = setInterval(myFunctionInterval, 5000);
});
function myFunctionInterval() {
alert("Hello (From Interval) !");
}
function clearLoop() {
clearInterval(myVar);
myVar = setInterval(myFunction, 5000);
}
function myFunction() {
alert("Hello (From Button)!");
}
</script>
</head>
<body>
<button onclick="clearLoop()">Click me</button>
</body>
</html>
Clear your setTimeout callback when the button is pressed, for example:
<button onClick='onClick()'/>
var onClick = () => {clearTimeout(yourTimer); yourFunc()}
本文标签: Avoid race condition in javascript (while using settimeout)Stack Overflow
版权声明:本文标题:Avoid race condition in javascript (while using settimeout) - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742049413a2417984.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论