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

Popular posts from this blog

c++ - Delete matches in OpenCV (Keypoints and descriptors) -

java - Could not locate OpenAL library -

sorting - opencl Bitonic sort with 64 bits keys -