setTimout vs SetInterval methods in Javascript

SetTimout vs SetInterval methods in Javascript: The setTimout() and setInterval() methods are both asynchronous functions, which means they do not pause code execution. The setTimeOut () method invokes a function once after a given amount of milliseconds, whereas the setInterval () method invokes code continuously after a given amount of milliseconds; it could run indefinitely.

To write any of the above methods, we first write the keywords in a set of parenthesis, then pass in the function you want to call and the number of milliseconds you want it to wait before being called, eg.

setTimeout(myFuncion, 1000) // or

setInterval(myFuncion, 1000)

This could also be assigned to a variable e.g

let timerId =  setInterval(myFuncion, 1000)

All the above blocks of code will call the function call “myFunction” after 1000 seconds (1 second). The “myfunction” is the function block to run, while “1000” is the number of milliseconds it has to wait before its runs.

The setTimeOut() method

For example, let’s say we want to pop up on the users’ screen after a given amount of milliseconds

This could be illustrated with the code below:

First, we declare a function called tryPremium

function tryPremium () {

    alert(“Would you love to try our premium services”) //The alert function

}

setTimeout(tryPremium, 2000); //This will be executed after 2000 milliseconds (2 seconds)

setTimeout(tryPremium, 6000); //This will be executed after 6000 milliseconds (6 seconds)

setTimeout(tryPremium, 15000); //This will be executed after 15000 milliseconds (15 seconds)

Assigning it to a variable we use:

let timerId;

timerId = setTimeout(tryPremium, 3000) // this could be at a click event or any other event.

See also  Protect Yourself from Cybersecurity Threats: 8 Must-Know Solutions

You could pass in an argument of the function after the millisecond which will look like this:

timerId = setTimeout(tryPremium, 3000, 1stArg, 2ndArg, …) // passing on arguments, This could lead to as many arguments as possible.

The above blocks of code will be executed one after the other depending on their amount of time, the first will be executed after, 2 seconds the second after 6 seconds while the third line of code will wait for 15 seconds before executing.

The setInterval() method

Just like we have stated above this executes code continuously after the given amount of time; let’s consider the example below:

We still declare our function and call it tryPremium

function tryPremium () {

    alert(“Would you love to try our premium services”) //The alert function

}

setInterval(tryPremium, 5000);

function tryPremium () {

    alert(“Would you love to try our premium services”)

}

Assigning it to a variable we use:

let timerId;

timerId = setInterval(tryPremium, 3000) // this could be at a click event or any other event.

You could pass in an argument of the function after the millisecond which will look like this:

timerId = setIntervaltryPremium, 3000, 1stArg, 2ndArg, …) // passing in arguments, This could be many arguments as possible

 This would popup an alert of  “Would you love to try our premium services” every 5000 milliseconds (5 seconds)

This is very helpful when you want your code to run repeatedly.

Note: The time you pass in is always in milliseconds

clearTimeout() AND clearInterval()

SetTimout vs SetInterval methods: Assuming you want the code block to run under a certain condition, this could also be cleared if the condition returns false.

See also  How Blockchain Technology and Cryptocurrencies are Unleashing the Power of Decentralization

To cancel or clear an interval or TimeOut you have to assign them to a timerId.

This can be achieved with the code below:

clearInterval(timerId)// you call clearInterval()  or clearTimeout() and pass in the timerId or variable you want to clear the timer from.

Read also: Windows Objects in Javascript: Explained

Like and Follow Us on Facebook: Flourish Tech-House

Originally posted 2023-02-20 22:06:12.

bryte

bryte

Leave a Reply

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