user interface - Start and stop loop in javascript with start and stop button -
this question has answer here:
i have start button when clicked runs function loops. how stopbtn.onclick stop running loop?
https://jsfiddle.net/vduxbnkj/
startbtn.onclick = function(){ runloop(); } function runloop(){ while(condition true){ getfoldercontentsloop(); } } function getfoldercontentsloop(){ //loop through folders & files looking .txt file , if "finished" delete files , folders }
if you're running simple for (..)
loop, cannot stopped via external influence. happening on same thread in javascript, unless code "ends" @ point , returns control browser while no ui interaction can happen. easiest way have "loop" via settimeout
or setinterval
:
interval = null; startbtn.onclick = function () { var = 0; interval = setinterval(function () { console.log(i++); // inside loop }, 1); }; stopbtn.onclick = function () { clearinterval(interval); };
Comments
Post a Comment