Do you really know how setimeout and setinterval work?

Do you really know how setimeout and setinterval work?

?? Hey Devs! Are you familiar with setTimeout and setInterval methods in JavaScript, but unsure about their inner workings and real-world applications? ?? No worries! This article is your guide to understanding these functions and their use cases. Let's dive in! ??

What is setTimeout? ??

Have you ever been stuck waiting for a meeting to start, only for it to be delayed? Just like that, setTimeout lets your code wait before executing. It's like telling your computer to chill for a bit before proceeding. For instance, waiting 5 seconds before displaying a message. Check out the syntax ??

setTimeout(function, delay, arg1, arg2, ...);        

Let's take a quick example

console.log('Hey!')

setTimeout(function () {
? console.log('Dev!');
}, 2000); // wait for 2 seconds

console.log('Goodbye!');        

  1. We start by logging 'Hey!' to the console.
  2. Then, using setTimeout, we schedule a function to run after a 2-second delay.
  3. Inside that function, we log 'Dev!' to the console.
  4. Finally, 'Goodbye!' is logged to the console.

In essence, it creates a brief pause, then prints 'Hey!', waits for 2 seconds, prints 'Dev!', and lastly logs 'Goodbye!'. This demonstrates how setTimeout lets you control the timing of actions in your code. ????

One thing to note is that setTimeout only fires once. Need repetition? Enter setInterval.

What is setInterval? ??

setInterval is like a metronome. It triggers your code repeatedly at set intervals. It's like instructing your computer to do something over and over on a timer. Let's break it down ??

setInterval(function, timeInterval);        

Let's also see an example

let count = 0

setInterval(() => {
? count++;
? console.log('Count is now ' + count);
}, 1000); // repeat every 1 second
        

  1. We initiate a count variable at 0.
  2. Using setInterval, we define an arrow function to execute repeatedly every 1 second (1000 milliseconds).
  3. Inside the function, we increment count and log the updated value along with 'Count is now ' to the console.

Here is the output

Count is now 1
Count is now 2
Count is now 3
Count is now 4
Count is now 5
...and on it goes!        

Use Cases! ??

When should you use these functions?

setTimeout can be your go-to for:

  • Delayed function execution, like showing notifications.
  • Web animations, such as delayed image fades.
  • Managing user input delays, like waiting for typing to finish before searching.

setInterval shines when:

  • Updating live elements, such as ticking clocks.
  • Keeping animations alive, like loading spinners.
  • Checking for data changes, such as polling for new messages.

?? To wrap it up, Devs, setTimeout, and setInterval are like your time-management allies in JavaScript. With setTimeout, you can make your code pause and then perform an action after a set delay. Meanwhile, setInterval lets you orchestrate repetitive actions at fixed intervals.

Remember:

  • setTimeout for delaying a single action.
  • setInterval for steady, recurring actions.

If you want to learn more about these powerful functions, here is a link to a well-detailed article I wrote on it




要查看或添加评论,请登录

Lawrence Amrasakpare的更多文章

社区洞察