Hi, I need to perform a specific function every 0.5 seconds, but do not use setTimeout / setInterval. Tell me how to do this using Date.now ()

on js, thanks in advance

  • homework? - lexxl
  • what I need this code for is just my business. For rudeness do not consider it. - Crok

1 answer 1

var interval = 500; //полсекунды var startDate = Date.now(); while (true) { var nowDate = Date.now(); if (nowDate-startDate >= interval) { startDate = nowDate; callSomeFunc(); } } 
  • one
    If I used such code in a real project, I would definitely fly for it. - Andy
  • What's the problem? - Crok
  • while (true) is a direct way to hang the work of the tab. - Andy
  • and if the condition of exit from the cycle is prescribed, then this code will not be so creepy? - Crok
  • While while (true) is spinning - CPU load is 100%. Exiting the loop will certainly fix the situation, but still. - Andy