Start and stop loop in javascript with start and stop button [duplicate]
This question already has an answer here:
Implementing a pause and resume mechanism for javascript loop execution
2 answers
I have a start button that when clicked runs a function that loops. How do I get a stopBTN.onClick to stop the running loop?
https://jsfiddle.net/vduxbnkj/
startBTN.onClick = function(){ runLoop(); }
function runLoop(){
while(condition true){
getFolderContentsLoop();
}
}
function getFolderContentsLoop(){
//loop through folders & files looking for .txt file and if "finished"
delete files and folders
}
javascript user-interface
marked as duplicate by Aadit M Shah
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
May 9 '15 at 10:14
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
This question already has an answer here:
Implementing a pause and resume mechanism for javascript loop execution
2 answers
I have a start button that when clicked runs a function that loops. How do I get a stopBTN.onClick to stop the running loop?
https://jsfiddle.net/vduxbnkj/
startBTN.onClick = function(){ runLoop(); }
function runLoop(){
while(condition true){
getFolderContentsLoop();
}
}
function getFolderContentsLoop(){
//loop through folders & files looking for .txt file and if "finished"
delete files and folders
}
javascript user-interface
marked as duplicate by Aadit M Shah
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
May 9 '15 at 10:14
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
Please use jsfiddle and show us the problem with your code / html
– Marcus Brunsten
Mar 20 '15 at 19:00
Loops run extremely quickly - what are you trying to accomplish with this? MaybesetInterval()
is what you're after?
– Scott
Mar 20 '15 at 19:01
I've written a script that loops through a directory folder ("Batch Folder") looking for a text file. When it finds the text folder it reads it and looks for "Finished". If finished it deletes all files and folders associated. As long as there are folders and files in the "Batch Folder" it will continue to loop. I was hoping to be able to stop it when I wanted.
– sbaden
Mar 20 '15 at 19:29
I've just updated my original code to show more info
– sbaden
Mar 20 '15 at 19:35
Here is the jsfiddle link - If I've done it correctly... jsfiddle.net/vduxbnkj
– sbaden
Mar 20 '15 at 19:45
add a comment |
This question already has an answer here:
Implementing a pause and resume mechanism for javascript loop execution
2 answers
I have a start button that when clicked runs a function that loops. How do I get a stopBTN.onClick to stop the running loop?
https://jsfiddle.net/vduxbnkj/
startBTN.onClick = function(){ runLoop(); }
function runLoop(){
while(condition true){
getFolderContentsLoop();
}
}
function getFolderContentsLoop(){
//loop through folders & files looking for .txt file and if "finished"
delete files and folders
}
javascript user-interface
This question already has an answer here:
Implementing a pause and resume mechanism for javascript loop execution
2 answers
I have a start button that when clicked runs a function that loops. How do I get a stopBTN.onClick to stop the running loop?
https://jsfiddle.net/vduxbnkj/
startBTN.onClick = function(){ runLoop(); }
function runLoop(){
while(condition true){
getFolderContentsLoop();
}
}
function getFolderContentsLoop(){
//loop through folders & files looking for .txt file and if "finished"
delete files and folders
}
This question already has an answer here:
Implementing a pause and resume mechanism for javascript loop execution
2 answers
javascript user-interface
javascript user-interface
edited Mar 20 '15 at 19:46
sbaden
asked Mar 20 '15 at 18:58


sbadensbaden
821518
821518
marked as duplicate by Aadit M Shah
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
May 9 '15 at 10:14
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
marked as duplicate by Aadit M Shah
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
May 9 '15 at 10:14
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
Please use jsfiddle and show us the problem with your code / html
– Marcus Brunsten
Mar 20 '15 at 19:00
Loops run extremely quickly - what are you trying to accomplish with this? MaybesetInterval()
is what you're after?
– Scott
Mar 20 '15 at 19:01
I've written a script that loops through a directory folder ("Batch Folder") looking for a text file. When it finds the text folder it reads it and looks for "Finished". If finished it deletes all files and folders associated. As long as there are folders and files in the "Batch Folder" it will continue to loop. I was hoping to be able to stop it when I wanted.
– sbaden
Mar 20 '15 at 19:29
I've just updated my original code to show more info
– sbaden
Mar 20 '15 at 19:35
Here is the jsfiddle link - If I've done it correctly... jsfiddle.net/vduxbnkj
– sbaden
Mar 20 '15 at 19:45
add a comment |
Please use jsfiddle and show us the problem with your code / html
– Marcus Brunsten
Mar 20 '15 at 19:00
Loops run extremely quickly - what are you trying to accomplish with this? MaybesetInterval()
is what you're after?
– Scott
Mar 20 '15 at 19:01
I've written a script that loops through a directory folder ("Batch Folder") looking for a text file. When it finds the text folder it reads it and looks for "Finished". If finished it deletes all files and folders associated. As long as there are folders and files in the "Batch Folder" it will continue to loop. I was hoping to be able to stop it when I wanted.
– sbaden
Mar 20 '15 at 19:29
I've just updated my original code to show more info
– sbaden
Mar 20 '15 at 19:35
Here is the jsfiddle link - If I've done it correctly... jsfiddle.net/vduxbnkj
– sbaden
Mar 20 '15 at 19:45
Please use jsfiddle and show us the problem with your code / html
– Marcus Brunsten
Mar 20 '15 at 19:00
Please use jsfiddle and show us the problem with your code / html
– Marcus Brunsten
Mar 20 '15 at 19:00
Loops run extremely quickly - what are you trying to accomplish with this? Maybe
setInterval()
is what you're after?– Scott
Mar 20 '15 at 19:01
Loops run extremely quickly - what are you trying to accomplish with this? Maybe
setInterval()
is what you're after?– Scott
Mar 20 '15 at 19:01
I've written a script that loops through a directory folder ("Batch Folder") looking for a text file. When it finds the text folder it reads it and looks for "Finished". If finished it deletes all files and folders associated. As long as there are folders and files in the "Batch Folder" it will continue to loop. I was hoping to be able to stop it when I wanted.
– sbaden
Mar 20 '15 at 19:29
I've written a script that loops through a directory folder ("Batch Folder") looking for a text file. When it finds the text folder it reads it and looks for "Finished". If finished it deletes all files and folders associated. As long as there are folders and files in the "Batch Folder" it will continue to loop. I was hoping to be able to stop it when I wanted.
– sbaden
Mar 20 '15 at 19:29
I've just updated my original code to show more info
– sbaden
Mar 20 '15 at 19:35
I've just updated my original code to show more info
– sbaden
Mar 20 '15 at 19:35
Here is the jsfiddle link - If I've done it correctly... jsfiddle.net/vduxbnkj
– sbaden
Mar 20 '15 at 19:45
Here is the jsfiddle link - If I've done it correctly... jsfiddle.net/vduxbnkj
– sbaden
Mar 20 '15 at 19:45
add a comment |
4 Answers
4
active
oldest
votes
If you're running a simple for (..)
loop, this cannot be stopped via external influence. Everything is happening on the same thread in Javascript, unless your code "ends" at some point and returns control to the browser for a while no UI interaction can happen. The easiest way to have a "loop" is via a setTimeout
or setInterval
:
interval = null;
startBTN.onclick = function () {
var i = 0;
interval = setInterval(function () {
console.log(i++); // this is inside your loop
}, 1);
};
stopBTN.onclick = function () {
clearInterval(interval);
};
What about checking a boolean in the for loop and if set to true by an external click event thenbreak
?
– furier
Mar 20 '15 at 19:49
And who is going to handle this external event and set the flag?
– deceze♦
Mar 21 '15 at 7:11
add a comment |
Javascript is single threaded and as long it is in a loop, it can't give control to other code to stop it. But if you have a special kind of loop that is implemented with setTimeout
:
function loopStep() {
...
}
function loop() {
loopStep();
setTimeout(loop, 0);
}
then you can add a flag to be able to stop loop's execution:
var flag = true;
function loop() {
if (!flag) return;
loopStep();
setTimeout(loop, 0);
}
and then you can define your stop function:
function stop() {
flag = false;
}
Thank you. I'll give this a try.
– sbaden
Apr 30 '15 at 17:27
add a comment |
I usually work around this by making my own boolean test as the while condition, like so:
var keepLooping = false;
while(!keepLooping){
document.getElementById("loopButton").onclick = function(){
keepLooping = true;
}
}
while(keepLooping){
//do something here
document.getElementById("loopButton").onclick = function(){
keepLooping = false;
}
}
Thank you. This looks like a great place to start. I'll give it a try.
– sbaden
Apr 30 '15 at 17:26
add a comment |
The only method I can think of is to create a boolean at the very beginning, and set stopBTN.onclick as a function that switches the variable. Then put an if condition that uses break if the boolean is switched.
var r = false;
startBTN.onClick = function(){ runLoop(); }
stopBTN.onClick = function(){r = true; }
function runLoop(){
while(condition true){
getFolderContentsLoop();
if(r){
break;
}
}
}
function getFolderContentsLoop(){
/*loop through folders & files looking for .txt file and if "finished"
delete files and folders*/
}
It's crude, but it should work.
add a comment |
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
If you're running a simple for (..)
loop, this cannot be stopped via external influence. Everything is happening on the same thread in Javascript, unless your code "ends" at some point and returns control to the browser for a while no UI interaction can happen. The easiest way to have a "loop" is via a setTimeout
or setInterval
:
interval = null;
startBTN.onclick = function () {
var i = 0;
interval = setInterval(function () {
console.log(i++); // this is inside your loop
}, 1);
};
stopBTN.onclick = function () {
clearInterval(interval);
};
What about checking a boolean in the for loop and if set to true by an external click event thenbreak
?
– furier
Mar 20 '15 at 19:49
And who is going to handle this external event and set the flag?
– deceze♦
Mar 21 '15 at 7:11
add a comment |
If you're running a simple for (..)
loop, this cannot be stopped via external influence. Everything is happening on the same thread in Javascript, unless your code "ends" at some point and returns control to the browser for a while no UI interaction can happen. The easiest way to have a "loop" is via a setTimeout
or setInterval
:
interval = null;
startBTN.onclick = function () {
var i = 0;
interval = setInterval(function () {
console.log(i++); // this is inside your loop
}, 1);
};
stopBTN.onclick = function () {
clearInterval(interval);
};
What about checking a boolean in the for loop and if set to true by an external click event thenbreak
?
– furier
Mar 20 '15 at 19:49
And who is going to handle this external event and set the flag?
– deceze♦
Mar 21 '15 at 7:11
add a comment |
If you're running a simple for (..)
loop, this cannot be stopped via external influence. Everything is happening on the same thread in Javascript, unless your code "ends" at some point and returns control to the browser for a while no UI interaction can happen. The easiest way to have a "loop" is via a setTimeout
or setInterval
:
interval = null;
startBTN.onclick = function () {
var i = 0;
interval = setInterval(function () {
console.log(i++); // this is inside your loop
}, 1);
};
stopBTN.onclick = function () {
clearInterval(interval);
};
If you're running a simple for (..)
loop, this cannot be stopped via external influence. Everything is happening on the same thread in Javascript, unless your code "ends" at some point and returns control to the browser for a while no UI interaction can happen. The easiest way to have a "loop" is via a setTimeout
or setInterval
:
interval = null;
startBTN.onclick = function () {
var i = 0;
interval = setInterval(function () {
console.log(i++); // this is inside your loop
}, 1);
};
stopBTN.onclick = function () {
clearInterval(interval);
};
answered Mar 20 '15 at 19:04
deceze♦deceze
399k64545701
399k64545701
What about checking a boolean in the for loop and if set to true by an external click event thenbreak
?
– furier
Mar 20 '15 at 19:49
And who is going to handle this external event and set the flag?
– deceze♦
Mar 21 '15 at 7:11
add a comment |
What about checking a boolean in the for loop and if set to true by an external click event thenbreak
?
– furier
Mar 20 '15 at 19:49
And who is going to handle this external event and set the flag?
– deceze♦
Mar 21 '15 at 7:11
What about checking a boolean in the for loop and if set to true by an external click event then
break
?– furier
Mar 20 '15 at 19:49
What about checking a boolean in the for loop and if set to true by an external click event then
break
?– furier
Mar 20 '15 at 19:49
And who is going to handle this external event and set the flag?
– deceze♦
Mar 21 '15 at 7:11
And who is going to handle this external event and set the flag?
– deceze♦
Mar 21 '15 at 7:11
add a comment |
Javascript is single threaded and as long it is in a loop, it can't give control to other code to stop it. But if you have a special kind of loop that is implemented with setTimeout
:
function loopStep() {
...
}
function loop() {
loopStep();
setTimeout(loop, 0);
}
then you can add a flag to be able to stop loop's execution:
var flag = true;
function loop() {
if (!flag) return;
loopStep();
setTimeout(loop, 0);
}
and then you can define your stop function:
function stop() {
flag = false;
}
Thank you. I'll give this a try.
– sbaden
Apr 30 '15 at 17:27
add a comment |
Javascript is single threaded and as long it is in a loop, it can't give control to other code to stop it. But if you have a special kind of loop that is implemented with setTimeout
:
function loopStep() {
...
}
function loop() {
loopStep();
setTimeout(loop, 0);
}
then you can add a flag to be able to stop loop's execution:
var flag = true;
function loop() {
if (!flag) return;
loopStep();
setTimeout(loop, 0);
}
and then you can define your stop function:
function stop() {
flag = false;
}
Thank you. I'll give this a try.
– sbaden
Apr 30 '15 at 17:27
add a comment |
Javascript is single threaded and as long it is in a loop, it can't give control to other code to stop it. But if you have a special kind of loop that is implemented with setTimeout
:
function loopStep() {
...
}
function loop() {
loopStep();
setTimeout(loop, 0);
}
then you can add a flag to be able to stop loop's execution:
var flag = true;
function loop() {
if (!flag) return;
loopStep();
setTimeout(loop, 0);
}
and then you can define your stop function:
function stop() {
flag = false;
}
Javascript is single threaded and as long it is in a loop, it can't give control to other code to stop it. But if you have a special kind of loop that is implemented with setTimeout
:
function loopStep() {
...
}
function loop() {
loopStep();
setTimeout(loop, 0);
}
then you can add a flag to be able to stop loop's execution:
var flag = true;
function loop() {
if (!flag) return;
loopStep();
setTimeout(loop, 0);
}
and then you can define your stop function:
function stop() {
flag = false;
}
edited Mar 20 '15 at 19:08
answered Mar 20 '15 at 19:01


JuniorCompressorJuniorCompressor
17.1k31951
17.1k31951
Thank you. I'll give this a try.
– sbaden
Apr 30 '15 at 17:27
add a comment |
Thank you. I'll give this a try.
– sbaden
Apr 30 '15 at 17:27
Thank you. I'll give this a try.
– sbaden
Apr 30 '15 at 17:27
Thank you. I'll give this a try.
– sbaden
Apr 30 '15 at 17:27
add a comment |
I usually work around this by making my own boolean test as the while condition, like so:
var keepLooping = false;
while(!keepLooping){
document.getElementById("loopButton").onclick = function(){
keepLooping = true;
}
}
while(keepLooping){
//do something here
document.getElementById("loopButton").onclick = function(){
keepLooping = false;
}
}
Thank you. This looks like a great place to start. I'll give it a try.
– sbaden
Apr 30 '15 at 17:26
add a comment |
I usually work around this by making my own boolean test as the while condition, like so:
var keepLooping = false;
while(!keepLooping){
document.getElementById("loopButton").onclick = function(){
keepLooping = true;
}
}
while(keepLooping){
//do something here
document.getElementById("loopButton").onclick = function(){
keepLooping = false;
}
}
Thank you. This looks like a great place to start. I'll give it a try.
– sbaden
Apr 30 '15 at 17:26
add a comment |
I usually work around this by making my own boolean test as the while condition, like so:
var keepLooping = false;
while(!keepLooping){
document.getElementById("loopButton").onclick = function(){
keepLooping = true;
}
}
while(keepLooping){
//do something here
document.getElementById("loopButton").onclick = function(){
keepLooping = false;
}
}
I usually work around this by making my own boolean test as the while condition, like so:
var keepLooping = false;
while(!keepLooping){
document.getElementById("loopButton").onclick = function(){
keepLooping = true;
}
}
while(keepLooping){
//do something here
document.getElementById("loopButton").onclick = function(){
keepLooping = false;
}
}
edited May 9 '15 at 8:42
answered Apr 30 '15 at 17:04
Jon DeWittJon DeWitt
353212
353212
Thank you. This looks like a great place to start. I'll give it a try.
– sbaden
Apr 30 '15 at 17:26
add a comment |
Thank you. This looks like a great place to start. I'll give it a try.
– sbaden
Apr 30 '15 at 17:26
Thank you. This looks like a great place to start. I'll give it a try.
– sbaden
Apr 30 '15 at 17:26
Thank you. This looks like a great place to start. I'll give it a try.
– sbaden
Apr 30 '15 at 17:26
add a comment |
The only method I can think of is to create a boolean at the very beginning, and set stopBTN.onclick as a function that switches the variable. Then put an if condition that uses break if the boolean is switched.
var r = false;
startBTN.onClick = function(){ runLoop(); }
stopBTN.onClick = function(){r = true; }
function runLoop(){
while(condition true){
getFolderContentsLoop();
if(r){
break;
}
}
}
function getFolderContentsLoop(){
/*loop through folders & files looking for .txt file and if "finished"
delete files and folders*/
}
It's crude, but it should work.
add a comment |
The only method I can think of is to create a boolean at the very beginning, and set stopBTN.onclick as a function that switches the variable. Then put an if condition that uses break if the boolean is switched.
var r = false;
startBTN.onClick = function(){ runLoop(); }
stopBTN.onClick = function(){r = true; }
function runLoop(){
while(condition true){
getFolderContentsLoop();
if(r){
break;
}
}
}
function getFolderContentsLoop(){
/*loop through folders & files looking for .txt file and if "finished"
delete files and folders*/
}
It's crude, but it should work.
add a comment |
The only method I can think of is to create a boolean at the very beginning, and set stopBTN.onclick as a function that switches the variable. Then put an if condition that uses break if the boolean is switched.
var r = false;
startBTN.onClick = function(){ runLoop(); }
stopBTN.onClick = function(){r = true; }
function runLoop(){
while(condition true){
getFolderContentsLoop();
if(r){
break;
}
}
}
function getFolderContentsLoop(){
/*loop through folders & files looking for .txt file and if "finished"
delete files and folders*/
}
It's crude, but it should work.
The only method I can think of is to create a boolean at the very beginning, and set stopBTN.onclick as a function that switches the variable. Then put an if condition that uses break if the boolean is switched.
var r = false;
startBTN.onClick = function(){ runLoop(); }
stopBTN.onClick = function(){r = true; }
function runLoop(){
while(condition true){
getFolderContentsLoop();
if(r){
break;
}
}
}
function getFolderContentsLoop(){
/*loop through folders & files looking for .txt file and if "finished"
delete files and folders*/
}
It's crude, but it should work.
answered Mar 20 '15 at 20:04
user2425429user2425429
998
998
add a comment |
add a comment |
Please use jsfiddle and show us the problem with your code / html
– Marcus Brunsten
Mar 20 '15 at 19:00
Loops run extremely quickly - what are you trying to accomplish with this? Maybe
setInterval()
is what you're after?– Scott
Mar 20 '15 at 19:01
I've written a script that loops through a directory folder ("Batch Folder") looking for a text file. When it finds the text folder it reads it and looks for "Finished". If finished it deletes all files and folders associated. As long as there are folders and files in the "Batch Folder" it will continue to loop. I was hoping to be able to stop it when I wanted.
– sbaden
Mar 20 '15 at 19:29
I've just updated my original code to show more info
– sbaden
Mar 20 '15 at 19:35
Here is the jsfiddle link - If I've done it correctly... jsfiddle.net/vduxbnkj
– sbaden
Mar 20 '15 at 19:45