admin管理员组文章数量:1023827
I want to stop a loop in a function from another function in JavaScript. Here is the code that I tried:
<a onmousedown="startLoop();" onmouseup="stopLoop();" >
Start loop
</a>
<script>
var static loop = 1;
function startLoop() {
while(loop ==1) {
}
alert("stop"); //This line is not executed
}
function stopLoop() {
loop = 2;
}
</script>
Here is the fiddle: /
I want to stop a loop in a function from another function in JavaScript. Here is the code that I tried:
<a onmousedown="startLoop();" onmouseup="stopLoop();" >
Start loop
</a>
<script>
var static loop = 1;
function startLoop() {
while(loop ==1) {
}
alert("stop"); //This line is not executed
}
function stopLoop() {
loop = 2;
}
</script>
Here is the fiddle: http://jsfiddle/A5h8k/4/
Share Improve this question edited Apr 10, 2013 at 14:50 MHDaouas asked Apr 10, 2013 at 14:40 MHDaouasMHDaouas 1793 silver badges14 bronze badges 1- 3 You can't; JavaScript is single-threaded so there's no way for anything else to execute while your loop is. – Anthony Grist Commented Apr 10, 2013 at 14:46
1 Answer
Reset to default 5This var static loop = 1;
is invalid, var loop = 1;
, but you start an infinite loop, so you won't be able to stop it this way.
Use setInterval instead
var int = self.setInterval(function(){myLoop()},100);
function myLoop() {
//do stuff
}
function stopLoop() {
window.clearInterval(int);
}
I want to stop a loop in a function from another function in JavaScript. Here is the code that I tried:
<a onmousedown="startLoop();" onmouseup="stopLoop();" >
Start loop
</a>
<script>
var static loop = 1;
function startLoop() {
while(loop ==1) {
}
alert("stop"); //This line is not executed
}
function stopLoop() {
loop = 2;
}
</script>
Here is the fiddle: /
I want to stop a loop in a function from another function in JavaScript. Here is the code that I tried:
<a onmousedown="startLoop();" onmouseup="stopLoop();" >
Start loop
</a>
<script>
var static loop = 1;
function startLoop() {
while(loop ==1) {
}
alert("stop"); //This line is not executed
}
function stopLoop() {
loop = 2;
}
</script>
Here is the fiddle: http://jsfiddle/A5h8k/4/
Share Improve this question edited Apr 10, 2013 at 14:50 MHDaouas asked Apr 10, 2013 at 14:40 MHDaouasMHDaouas 1793 silver badges14 bronze badges 1- 3 You can't; JavaScript is single-threaded so there's no way for anything else to execute while your loop is. – Anthony Grist Commented Apr 10, 2013 at 14:46
1 Answer
Reset to default 5This var static loop = 1;
is invalid, var loop = 1;
, but you start an infinite loop, so you won't be able to stop it this way.
Use setInterval instead
var int = self.setInterval(function(){myLoop()},100);
function myLoop() {
//do stuff
}
function stopLoop() {
window.clearInterval(int);
}
本文标签: htmlStop a loop in a function from another function in JavaScriptStack Overflow
版权声明:本文标题:html - Stop a loop in a function from another function in JavaScript - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745565138a2156403.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论