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!
5 More HTML5 APIs You Didn’t Know Existed
The HTML5 revolution has provided us some awesome JavaScript and HTML APIs. Some are APIs we knew we’ve needed for years, others are cutting edge mobile and desktop helpers. Regardless of API strength or purpose, anything to help us better do our job is a…
JavaScript Promise API
While synchronous code is easier to follow and debug, async is generally better for performance and flexibility. Why “hold up the show” when you can trigger numerous requests at once and then handle them when each is ready? Promises are becoming a big part of the JavaScript world…
QuickBoxes for Dojo
Adding to my mental portfolio is important to me. First came MooTools, then jQuery, and now Dojo. I speak often with Peter Higgins of Dojo fame and decided it was time to step into his world. I chose a simple but useful plugin…
Add Site Screenshots for External Links Using MooTools Tooltips
Before you send your user to an unknown external website, why not provide them a screenshot of the site via a tooltip so they may preview the upcoming page? Here’s how you can do just that using MooTools. The MooTools JavaScript The first step is to grab…
Source link