setInterval and setTimeout - timing events - Beau teaches JavaScript

Timing Events in JavaScript: Understanding Set Timeout and Set Interval

There are two timing events that are part of the window object that both allow execution of code at specific time intervals. These two timing events are set timeout and set interval, each allowing for the execution of code to occur at regular or one-time intervals.

Set Timeout: Executing Code After a Specific Delay

The set timeout function is used to execute a piece of code after a specified delay has passed. To use this function, you pass in a function name and the desired delay in milliseconds. For example, `window.setTimeout(functionName, milliseconds)`. When the code within the function is executed, it will run after the specified delay has passed.

Let's take a look at an example of using the set timeout function to execute a piece of code that prints "goodbye" followed by "hello". We'll create a simple script like this:

```javascript

window.settimeout(function() {

console.log('hello');

console.log('goodbye');

}, 3000);

```

In this example, after 3 seconds (3000 milliseconds), the function will execute and print "hello" followed by "goodbye". This demonstrates how the set timeout function can be used to execute code at a specific interval.

Stopping a Set Timeout Function

One of the key features of the set timeout function is that it can be stopped from executing. To stop a set timeout function, you need to assign the returned value (a timer ID) to a variable and then call `clearTimeout` with that variable as an argument.

For example:

```javascript

let timeoutId = window.settimeout(function() {

console.log('hello');

}, 3000);

window.clearTimeout(timeoutId);

```

In this example, we've assigned the returned value (a timer ID) to a variable called `timeoutId`. We can then call `clearTimeout` with that variable as an argument to stop the function from executing.

Set Interval: Executing Code at Regular Intervals

The set interval function is used to execute a piece of code at regular intervals, allowing for the execution of code repeatedly until it's stopped. To use this function, you pass in a function name and the desired delay between executions in milliseconds. For example, `window.setInterval(functionName, milliseconds)`.

Let's take a look at an example of using the set interval function to execute a piece of code that increments a counter every second:

```javascript

let counter = 0;

function counterFunction() {

console.log(counter);

counter++;

}

window.setInterval(counterFunction, 1000);

// Clearing the interval later

let clearIntervalId = window.setInterval(counterFunction, 1000);

window.clearInterval(clearIntervalId);

```

In this example, we've created a simple script that executes a function called `counterFunction` every second (1000 milliseconds). The function increments a counter and logs it to the console. We can also see how clearing the interval using `clearInterval` stops the code from executing.

Using Set Interval with a Button

One of the practical applications of set interval is in creating interactive interfaces, such as buttons that trigger events at regular intervals. In this case, we can use an on-click event to clear the interval when the button is clicked.

Here's an example:

```javascript

let counter = 0;

function counterFunction() {

console.log(counter);

counter++;

}

window.setInterval(counterFunction, 1000);

// Clearing the interval with a button click

let clearIntervalId = window.setInterval(counterFunction, 1000);

document.getElementById("stop-button").addEventListener("click", function() {

window.clearInterval(clearIntervalId);

});

```

In this example, we've created a script that executes a counter function every second using set interval. We've also added an on-click event to a button called "stop-button" that clears the interval when clicked.

Conclusion

Timing events in JavaScript are powerful tools for controlling the execution of code at specific intervals or one-time occurrences. Set timeout and set interval functions allow developers to execute code repeatedly until it's stopped, making them useful for creating interactive interfaces and managing complex logic. By understanding how these timing events work, developers can create more efficient and effective scripts that achieve their desired outcomes.

"WEBVTTKind: captionsLanguage: enthere are two timing events that are part of the window object that both allow execution of code at specific time time intervals the two timing events are set timeout and set interval for set timeout you pass in a function in milliseconds and the program will wait the specified number of milliseconds and execute the function let's see some code okay here's the set timeout function it could be window.settimeout but the window is implied you pass in the name of the function which is by and then here's the function down here you also pass in the milliseconds here so after 3 000 milliseconds which is three seconds it's going to run the function by which this is just going to console.log goodbye but before that it's going to console.log hello so let's run that one two three so you saw it said hello and then goodbye it is possible to stop the function from running after the set timeout is called you must assign the set timeout to a variable so luvvar so we've set that to var timeout id and then we're going to call cleartimeout and pass in the variable so let's try that it's going to say hello but it will never say goodbye because we've cleared the timeout the set interval function is similar you pass in a function in milliseconds but the program will execute the function continuously waiting the specified number of milliseconds between each function here we're creating a timer we're going to set the count to zero and we're going to call set interval pass in the counter function and 1000 milliseconds that's one second so every one second it's going to call this counter function where it's going to log count it's also going to increment count one and we're going to set it to an int id up here i already have a button so in the button we have an on click event that says clear interval with an int id the end id is down there we can stop the timer by clicking the stop timer button and it won't go on forever so let's run that and see how that works well you can see it's counting every second but if i click the stop time button it stops so clear interval just stops the function in the set interval from running well that's all for now thanks for watching my name is beau carnes don't forget to subscribe and remember use your code for goodthere are two timing events that are part of the window object that both allow execution of code at specific time time intervals the two timing events are set timeout and set interval for set timeout you pass in a function in milliseconds and the program will wait the specified number of milliseconds and execute the function let's see some code okay here's the set timeout function it could be window.settimeout but the window is implied you pass in the name of the function which is by and then here's the function down here you also pass in the milliseconds here so after 3 000 milliseconds which is three seconds it's going to run the function by which this is just going to console.log goodbye but before that it's going to console.log hello so let's run that one two three so you saw it said hello and then goodbye it is possible to stop the function from running after the set timeout is called you must assign the set timeout to a variable so luvvar so we've set that to var timeout id and then we're going to call cleartimeout and pass in the variable so let's try that it's going to say hello but it will never say goodbye because we've cleared the timeout the set interval function is similar you pass in a function in milliseconds but the program will execute the function continuously waiting the specified number of milliseconds between each function here we're creating a timer we're going to set the count to zero and we're going to call set interval pass in the counter function and 1000 milliseconds that's one second so every one second it's going to call this counter function where it's going to log count it's also going to increment count one and we're going to set it to an int id up here i already have a button so in the button we have an on click event that says clear interval with an int id the end id is down there we can stop the timer by clicking the stop timer button and it won't go on forever so let's run that and see how that works well you can see it's counting every second but if i click the stop time button it stops so clear interval just stops the function in the set interval from running well that's all for now thanks for watching my name is beau carnes don't forget to subscribe and remember use your code for good\n"