JavaScript SetInterval() Function Tutorial with Examples – POFTUT

JavaScript SetInterval() Function Tutorial with Examples

JavaScriptprovides different function for the user experience. SetInterval() is a function used to specify the interval between given actions and fire these actions after specified intervals. In this tutorial, we will learn how to create intervals or delays for specified actions, functions and clear them.

SetInterval() Syntax

We will first start by learning the SetInterval() function syntax.

  • CODE/FUNCTION is the code, action or function we want to fire after the specified delay
  • INTERVAL is the delay or interval time we want to wait to fire code. DELAY value is specified in milliseconds which means to wait 3 seconds we should provide 3000 to the setInterval() function.
  • PARAM1 is optional where it can be used to send parameter to the CODE/FUNCTION

SetInterval() Return Value

After using the setInterval() function it will return a numeric value. This numeric value is the create interval instance ID which can be expressed as intervalID. IntervalID can be used to change settings like clearing the interval.

Timing with SetInterval() Function

SetInterval() function is provided by the Window and Worker objects. We will start with a simple example. We will run the function named ChangeColor() in every 10 seconds which will print some text to the web browser console. We will use 10000 milliseconds to represent 10 seconds.

Timing with SetInterval() Function

Timing with SetInterval() Function

Stop Execution or Timer with clearInterval() Function

When the timer starts with SetInterval() function it will run forever unless we do not stop explicitly. We can stop the time with the clearInterval() function. In a web page, there may be more than one intervals set. So we have to provide the interval we want to stop or cancel by providing its IntervalID. In this example we will create a button which will call the clearInterval() function by providing the MyIntervalID like below.

Stop Execution or Timer with clearInterval() Function

Stop Execution or Timer with clearInterval() Function

Pass Parameters To The Function

We can pass parameters to the delayed function. In this example, we will provide the name parameter to the interval function named PrintMyName().

Pass Parameters To The Function

Pass Parameters To The Function

SetInterval() vs SetTimeout()

JavaScript also provides the setTimeout() function in order to set delays between the execution of the given function or code. So which one should we use and what is the little difference between setInterval() and setTimeout() function. setInterval() function will run like a scheduled job every given interval without any latency but setTimeout() function is a bit different where function, code runs and then the given delay set again. If the function execution is 5 seconds and delays it 10 seconds the next start will take 15 seconds in total with the  setTimeout() function.

Leave a Reply

Your email address will not be published. Required fields are marked *

Enjoy this blog? Please spread the word :)