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
Add a ment  | 

1 Answer 1

Reset to default 5

This 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
Add a ment  | 

1 Answer 1

Reset to default 5

This 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