Start and stop loop in javascript with start and stop button [duplicate]












1
















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
}









share|improve this question















marked as duplicate by Aadit M Shah javascript
Users with the  javascript badge can single-handedly close javascript questions as duplicates and reopen them as needed.

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? 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


















1
















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
}









share|improve this question















marked as duplicate by Aadit M Shah javascript
Users with the  javascript badge can single-handedly close javascript questions as duplicates and reopen them as needed.

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? 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
















1












1








1









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
}









share|improve this question

















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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 javascript
Users with the  javascript badge can single-handedly close javascript questions as duplicates and reopen them as needed.

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 javascript
Users with the  javascript badge can single-handedly close javascript questions as duplicates and reopen them as needed.

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? 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





















  • 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



















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














4 Answers
4






active

oldest

votes


















6














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);
};





share|improve this answer
























  • 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



















1














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;
}





share|improve this answer


























  • Thank you. I'll give this a try.

    – sbaden
    Apr 30 '15 at 17:27



















0














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;
}
}





share|improve this answer


























  • Thank you. This looks like a great place to start. I'll give it a try.

    – sbaden
    Apr 30 '15 at 17:26



















-1














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.






share|improve this answer






























    4 Answers
    4






    active

    oldest

    votes








    4 Answers
    4






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    6














    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);
    };





    share|improve this answer
























    • 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
















    6














    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);
    };





    share|improve this answer
























    • 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














    6












    6








    6







    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);
    };





    share|improve this answer













    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);
    };






    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Mar 20 '15 at 19:04









    decezedeceze

    399k64545701




    399k64545701













    • 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



















    • 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

















    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













    1














    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;
    }





    share|improve this answer


























    • Thank you. I'll give this a try.

      – sbaden
      Apr 30 '15 at 17:27
















    1














    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;
    }





    share|improve this answer


























    • Thank you. I'll give this a try.

      – sbaden
      Apr 30 '15 at 17:27














    1












    1








    1







    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;
    }





    share|improve this answer















    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;
    }






    share|improve this answer














    share|improve this answer



    share|improve this answer








    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



















    • 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











    0














    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;
    }
    }





    share|improve this answer


























    • Thank you. This looks like a great place to start. I'll give it a try.

      – sbaden
      Apr 30 '15 at 17:26
















    0














    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;
    }
    }





    share|improve this answer


























    • Thank you. This looks like a great place to start. I'll give it a try.

      – sbaden
      Apr 30 '15 at 17:26














    0












    0








    0







    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;
    }
    }





    share|improve this answer















    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;
    }
    }






    share|improve this answer














    share|improve this answer



    share|improve this answer








    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



















    • 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











    -1














    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.






    share|improve this answer




























      -1














      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.






      share|improve this answer


























        -1












        -1








        -1







        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.






        share|improve this answer













        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.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Mar 20 '15 at 20:04









        user2425429user2425429

        998




        998















            Popular posts from this blog

            MongoDB - Not Authorized To Execute Command

            Npm cannot find a required file even through it is in the searched directory

            in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith