What is the best practise for php sleep function in javascript? -
for example in php put in loop sleep function 5 min pause execution loop new fetch result database or other instances how can done in javascript way set timeout , call same function inside it, thank you.
as pointed out in comments above, javascript single-threaded , not possible unless wrap code want execute after delay in it's own function.
you make kind of progress storage system whereby keep track of stage want different pieces of code execute based on pause cycle, example:
var stage = 0; setinterval(function() { executestage(++stage); // iterate stage }, 300000); // 5 mins var executestage = function(stagetoexecute) { // put code want execute @ every stage here console.log('this code execute no matter stage set'); // execute code specific stage switch (stagetoexecute) { case 1: // put stage 1 code here console.log('stage 1 executes here'); break; case 2: // put stage 2 code here console.log('stage 2 executes here'); break; // , on } }
the above idea, depends on use case has not been explained in question. should bear in mind first stage won't execute until 5 minutes have passed setinterval
, if want execute stage wait 5 minutes, you'll need add call executestage(++stage)
outside of setinterval
call.
Comments
Post a Comment