Updated April 6, 2023
Introduction to setInterval TypeScript
setInterval TypeScript is a method used to repeat specific function with every given time intervals. setInterval() will evaluate expressions or calls a function at certain intervals. This method will continue calling function until window is closed or the clearInterval() method is called and returns a non-zero number which identifies created timer or a numeric value. TypeScript has offered a number of functions that allow code to be executed asynchronously after a certain time intervals elapse and execute repeatedly until the user gives a stop. Asynchronous code runs on the main function.
Syntax of setInterval TypeScript
Given below is the syntax of setInterval() in TypeScript:
Or
let timer = setInterval(callback function, delay,[arg1, arg2, arg3, ...]);
- As per the syntax, function() is actually the callback function to be executed for each millisecond.
- time_in_milliseconds, involves the delay that the timer should actually have a delay between the execution of callback functions.
- arg1, arg2, arg3, … are the arguments passed to callback function.
- setInterval() function will return a non zero numeric that will identify created timer. It works similar to setTimeout() but executes callback repeatedly for each specific delay.
Examples
Given below are the examples of setInterval TypeScript:
Example #1: setInterval() timer
Code:
<!DOCTYPE html>
<body>
<p>Once this code is executed, after 5000 milliseconds, data is logged</p>
<p>New alert will open once the previous one is closed after 5 seconds! which actually stops timer!</p>
<script>
"use strict";
let timerId = setInterval(() => alert('10 seconds delay here!'), 5000);
setTimeout(() => { clearInterval(timerId); alert('Timer stops!'); }, 5000);
</script>
</body>
</html>
Output:
So here you have the input logged on after 5 seconds of the code execution.
And within another 5 seconds, timer is stopped as below.
setInterval() comes into the picture when the user wants to run code repeatedly, e.g., in cases of animation or any recursive algorithms.
It works in a similar way as setTimeout() but the only difference is that the function passed as the first parameter is repeatedly executed with no less than the milliseconds value given by the other argument. User can pass any number of parameters to the function that is being executed like subsequent parameters.
Example #2: setInterval() for displaying timer
Code:
<!DOCTYPE html>
<html>
<head>
<title>Clock using setInterval() method</title>
</head>
<body>
<p>Timer is ticking here!</p>
<p class="time"></p>
<script>
function displayTimer() {
let dateVal = new Date();
let timeVal = dateVal.toLocaleTimeString();
document.querySelector('.time').textContent = timeVal;
}
displayTimer();
const createClock = setInterval(displayTimer, 1000);
</script>
</body>
</html>
Output:
Here we are displaying timer with interval of 1 millisecond.
In the output, it shows as a constant value, but on execution, you can see that timer will be ticking for each second i.e., the function used along with setInterval() is called repeatedly. Here, setInterval() method will return an identifiable value which can be used in further methods such as to cleat interval or stop timer. setInterval() timer will keep on running the code, until user does something to stop. Users should stop such kind as there may be errors and the browser will not be able to complete the other tasks. Returned value from setInterval() method is passed to clearInterval() method, to stop Intervals.
Recursion with setInterval() method: Interval time chosen includes time taken to execute the given part of code user wants to run. For e.g., let us say, code takes 60 milliseconds to run, hence the rest 40 milliseconds will end up as an interval.
Example #3: setInterval() for changing text color
Code:
<!DOCTYPE html>
<html>
<head>
<title>setInterval() to bring changes in text color, along with clearInterval to stop</title>
<script>
var interval;
function colorChange() {
interval = setInterval(text, 3000);
}
function text() {
var Ele = document.getElementById('boxID');
if(Ele.style.color === 'orange') {
Ele.style.color = 'blue'
} else {
Ele.style.color = 'orange'
}
}
function stopColorChange() {
clearInterval(interval);
}
</script>
</head>
<body onload="colorChange();">
<div id="boxID">
<p>Text color keeps changing for every interval</p>
</div>
<button onclick="stopColorChange();">Click here to Stop!</button>
</body>
</html>
Output:
Here, once this code is executed, after 3 milliseconds, text color will change to orange.
And after other 3 milliseconds, text color changes to blue, as below based on the if condition.
Once you click the stop button, changing text colors would stop.
Conclusion
With this, we shall conclude the topic ‘setInterval TypeScript’. We have seen what setInterval TypeScript is and explained how it works with few examples. We have seen the syntax of setInterval() method to implement programs. We also have clearInterval() method to stop the interval timer. The above examples will give a clear picture of setInterval() is to be implemented.
Recommended Articles
We hope that this EDUCBA information on “setInterval TypeScript” was beneficial to you. You can view EDUCBA’s recommended articles for more information.