Immediately Executing setInterval with JavaScript

Employing setInterval for condition polling has really been useful over the years. Whether polling on the client or server sides, being reactive to specific conditions helps to improve user experience. One task I recently needed to complete required that my setInterval immediately execute and then continue executing. The conventional and best way to immediately call […]

The post Immediately Executing setInterval with JavaScript appeared first on David Walsh Blog.


This content originally appeared on David Walsh Blog and was authored by David Walsh

Employing setInterval for condition polling has really been useful over the years. Whether polling on the client or server sides, being reactive to specific conditions helps to improve user experience. One task I recently needed to complete required that my setInterval immediately execute and then continue executing.

The conventional and best way to immediately call a function at the beginning of a setInterval is to actually call the function before the initial setInterval` is called:

myFunction();
setInterval(myFunction, 1000); // Every second

If you truly want to isolate the function call to the setInterval, you can use this trick of self-executing function that returns itself:

// Use a named function ...
setInterval(function myFunction() {
  // Do some stuff
  // ...

  // ... then return this function
  return myFunction;

// () self-executes the function
}(), 3000)

The down side to this pattern is that it causes a maintenance issue, where the next developer doesn’t understand what is going on.

Maintenance is an important part of being a good engineer, so at the very least, documentation in the form of comments or a helper function should be required. If you really want to have a self-executing setInterval, you’ve got it!

The post Immediately Executing setInterval with JavaScript appeared first on David Walsh Blog.


This content originally appeared on David Walsh Blog and was authored by David Walsh


Print Share Comment Cite Upload Translate Updates
APA

David Walsh | Sciencx (2022-01-14T02:53:15+00:00) Immediately Executing setInterval with JavaScript. Retrieved from https://www.scien.cx/2022/01/14/immediately-executing-setinterval-with-javascript/

MLA
" » Immediately Executing setInterval with JavaScript." David Walsh | Sciencx - Friday January 14, 2022, https://www.scien.cx/2022/01/14/immediately-executing-setinterval-with-javascript/
HARVARD
David Walsh | Sciencx Friday January 14, 2022 » Immediately Executing setInterval with JavaScript., viewed ,<https://www.scien.cx/2022/01/14/immediately-executing-setinterval-with-javascript/>
VANCOUVER
David Walsh | Sciencx - » Immediately Executing setInterval with JavaScript. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2022/01/14/immediately-executing-setinterval-with-javascript/
CHICAGO
" » Immediately Executing setInterval with JavaScript." David Walsh | Sciencx - Accessed . https://www.scien.cx/2022/01/14/immediately-executing-setinterval-with-javascript/
IEEE
" » Immediately Executing setInterval with JavaScript." David Walsh | Sciencx [Online]. Available: https://www.scien.cx/2022/01/14/immediately-executing-setinterval-with-javascript/. [Accessed: ]
rf:citation
» Immediately Executing setInterval with JavaScript | David Walsh | Sciencx | https://www.scien.cx/2022/01/14/immediately-executing-setinterval-with-javascript/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.