setInterval vs setTimeout

Both are Javascript’s timing events.

setInterval(function(), milliseconds):

  • Executes a function repeatedly, with a fixed time delay between each call to that function. (useful for countdown)
  • clearInterval() takes an in-scope variable which points to the setInterval method and stops the function from looping.


setTimeout(function(), milliseconds):

  • Executes a function once, after waiting a specified delay.
  • clearTimeout() stops the execution, thus disabling it from running again in the future.


  • 1000 milliseconds = 1 second
setInterval vs setTimeout

$(document).ready() vs. window.onload()

SUMMARY: Run Javascript after the DOM has loaded but before the entire page has fully loaded (for faster loading time) because your code relies on working with the DOM.  This can be done with $( document).ready() in jQuery.

$(document).ready() in jQuery executes when the DOM (Document object model) is loaded on your page. DOM means all the html tags/script. It will not wait for the images, frames to be fully loaded.  It is the earliest stage in page load process and executes a bit earlier than window.onload(), thus less time-consuming for the browser.

window.onload() in JavaScript executes when all the content on your page is fully loaded including the DOM, asynchronous javascript, frames, images, and other assets.


  1. Browser makes a request to download the page
  2. Raw markup and DOM is loading (does not include external sources such as images and stylesheets).  DOMContentLoaded event occurs at the end of this stage and you can set your code to run at this time with $(document).ready().
  3. Page is fully loaded with images and stylesheets.  Load event occurs at the end of this stage and you can execute your code to run at this time with window.onload().  (You rarely need to do this unless you need to work with the images or assets in your code).
$(document).ready() vs. window.onload()


.forEach() executes the callback function on each item of the array once.

arr.forEach(function callback(currentValue, index, array) {
    //your iterator
}[, thisArg]);

How to use:

  1. Create a callback function
    function logArrayElements(element, index, array) {
      console.log('a[' + index + '] = ' + element);
  2. Apply callback function to .forEach() method
    [2, 5, , 9].forEach(logArrayElements);
    // a[0] = 2
    // a[1] = 5
    // a[3] = 9

* This can be combined into one step if the callback function is simple.