Javatpoint Logo
Javatpoint Logo

JavaScript timer

In JavaScript, a timer is created to execute a task or any function at a particular time. Basically, the timer is used to delay the execution of the program or to execute the JavaScript code in a regular time interval. With the help of timer, we can delay the execution of the code. So, the code does not complete it's execution at the same time when an event triggers or page loads.

The best example of the timer is advertisement banners on websites, which change after every 2-3 seconds. These advertising banners are changed at a regular interval on the websites like Amazon. We set a time interval to change them. In this chapter, we will show you how to create a timer.

JavaScript offers two timer functions setInterval() and setTimeout(), which helps to delay in execution of code and also allows to perform one or more operations repeatedly. We will discuss both the timer functions in detail as well as their examples.

Examples

Below are some examples of JavaScript timer using these functions.

setTimeout()

The setTimeout() function helps the users to delay the execution of code. The setTimeout() method accepts two parameters in which one is a user-defined function, and another is the time parameter to delay the execution. The time parameter holds the time in milliseconds (1 second = 1000 milliseconds), which is optional to pass.

The basic syntax of setTimeout() is:

We will use the setTimeout() function to delay the printing of message for 3 seconds. The message will display on the web after 3 seconds of code execution rather than immediately. Now, let's look at the code below to see how it works:

Execution of code after a delay

Test it Now

Output

The above code will execute in two sections. Firstly, the HTML part of the code will execute, where by clicking on Click Here button the remaining JavaScript code will execute after 3 seconds. See the output below:

JavaScript timer

On clicking the Click Here button, the remaining code will execute after 3 seconds. A message Welcome to javaTpoint will display on the web after 3 seconds (3000 milliseconds).

JavaScript timer

setInterval()

The setInterval method is a bit similar to the setTimeout() function. It executes the specified function repeatedly after a time interval. Or we can simply say that a function is executed repeatedly after a specific amount of time provided by the user in this function. For example - Display updated time in every five seconds.

The basic syntax of setInterval() is:

Similar to setTimeout() method, it also accepts two parameters in which one is a user-defined function, and another is a time-interval parameter to wait before executing the function. The time-interval parameter holds the amount of time in milliseconds (1 second = 1000 milliseconds), which is optional to pass. Now, see the code below how this function works:

Execution of code at a regular interval

Test it Now

Output

On executing the above code, the response will be a simple HTML statement without any time string like below output:

JavaScript timer

After 4 seconds, the JavaScript function will call and start displaying the time. This will repeatedly display your system time in every four seconds.

JavaScript timer

Example

One more example of setInterval() methods for displaying a message string after every 4 seconds continuously.

Test it Now

Output

On executing the above code, a message with a button will show on the browser. Click on this button to process more and see what happens next.

JavaScript timer

By clicking on this Click Here button, the waitAndshow() will start printing the message Welcome to Javatpoint repeatedly on the web in every 4 seconds.

JavaScript timer

You have seen how to create timer or set time interval. Sometimes, we need to cancel these timers. JavaScript offers the in-built function to clear the timer, which are as follows:

Cancel or Stop the timer

JavaScript offers two functions clearTimeout() and clearInterval() to cancel or stop the timer and halt the execution of code. The setTimeout() and setInterval() both return a unique IDs. These IDs are used by the clearTimeout() and clearInterval() to clear the timer and stop the code execution beforehand. They both take only one parameter, i.e., ID.

Example

In this example, we will use clearTimeout() to clear the timer set by with setTimeout() function. Look at the example how clearInterval() work with setInterval().

Disable the regular interval

Test it Now

Output

By executing the above code, the current system time will start showing on the web with 3 seconds of regular interval. This page also has a button to disable this timer.

JavaScript timer

The timer will show the updated time after every three seconds. If you click on this Stop Clock button, the timer will be disabled and stop showing the updated time.

JavaScript timer




Youtube For Videos Join Our Youtube Channel: Join Now

Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA