


Timer usage of setTimeOut and setInterval in Javascript_javascript skills
Javascript’s setTimeOut and setInterval functions are widely used. They are used to handle delayed and scheduled tasks. For example, a login box pops up after opening a web page for a period of time, and the page sends asynchronous requests to obtain the latest data at regular intervals, etc. But their applications are different.
The setTimeout() method is used to call a function or calculated expression after a specified number of milliseconds, while setInterval() loops to call a function or expression every specified number of milliseconds until clearInterval clears it. In other words, setTimeout() is only executed once, and setInterval() can be executed multiple times. The parameters of the two functions are also the same. The first parameter is the code or handle to be executed, and the second parameter is the number of milliseconds to delay.
setTimeOut usage
The usage of the setTimeout function is as follows:
var timeoutID = window.setTimeout(func, [delay, param1, param2, ...]);
var timeoutID = window.setTimeout(code, [delay]);
timeoutID: timer ID number, which can be used to clear the timer in the clearTimeout() function.
func: the function being executed.
code: (alternative syntax) a string of code to be executed.
delay: delay time, in milliseconds. If not specified, defaults to 0.
We can use window.setTimeout or setTimeout. The two writing methods are basically the same, except that window.setTimeout refers to the setTimeout function as a property of the global window object.
Application example:
function timeout(){
Document.getElementById('res').innerHTML=Math.floor(Math.random()*100 1);
}
setTimeout("timeout()",5000);
When the code is executed, the timeout() function will be called after 5 seconds. Click to see the demonstration.
setInterval usage
The parameters and usage of the setInterval function are the same as the setTimeout function. Please refer to the usage introduction of the setTimeout function above. The difference is that setInterval executes the func or code code at regular intervals.
Application example:
var tt = 10;
function timego(){
tt--;
Document.getElementById("tt").innerHTML = tt;
If(tt==0){
window.location.href='/';
return false;
}
}
var timer = window.setInterval("timego()",1000);
The function timego() defines the content displayed by the page element #tt. When tt equals 0, the page is directed to the home page. Then we define a timer timer and use setInterval() to call timego() every 1 second. In this way, timego will be executed 10 times, and each time the number tt will be reduced by 1 until it is 0. Then if you want to stop the timer, you can use the following code:
window.clearInterval(timer);
When the code is executed, the page will jump to the homepage after 10 seconds
In fact, setTimeout() can also execute a function repeatedly at regular intervals, but we still simply use setTimeOut and setInterval differently. In addition, JavaScript runs in the browser's JavaScript engine in a single-threaded manner. In actual applications, complex tasks need to be queued for execution, which may lead to inaccurate timers. This issue needs to be considered in large-scale applications. This article does not Do deep research.
The above is the entire content of this article, I hope you all like it.

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











The difference between settimeout and setInterval: 1. Trigger time, settimeout is one-time, it executes the function once after setting the delay time, while setinterval is repetitive, it will execute the function repeatedly at the set time interval; 2. Execution times, settimeout is only executed once, and setinterval will be executed repeatedly until canceled.

How to use setInterval function to execute code regularly? In JavaScript, the setInterval function is a very useful function, which can execute a piece of code regularly. Through the setInterval function, we can repeatedly execute specified code within a specific time interval. This article will introduce in detail how to use the setInterval function and provide specific code examples. 1. The basic syntax of the setInterval function is as follows: setInterv

The usage of setinterval is "setInterval(function, delay);", "function" is the function to be executed, which can be a function expression or function reference, and "delay" is the time interval between executing functions, in milliseconds. setInterval is a function in JavaScript that is used to execute code periodically. It accepts a function and a time interval as parameters, and will execute the function repeatedly according to the specified time interval.

The basic syntax of the Window.setInterval() method is "window.setInterval(function, delay)", function is the function or code block to be executed repeatedly, and delay is the time interval between each execution, in milliseconds. This method is a method in JavaScript used to repeatedly execute a specified function or code at a scheduled time. Its use is very simple. You only need to pass in the function or code block to be executed and the time interval for repeated execution.

To use the clearTimeout function in JavaScript to cancel the setTimeout timer, you need specific code examples. In JavaScript, the setTimeout function is used to execute a specific code after a specified time delay. The setInterval function is used to repeatedly execute a specific code within a specified time interval. However, in some cases we may need to cancel the timer before it executes. In this case, you can use c

The setInterval function is a timer function in JavaScript that allows you to set an interval and execute specified code after each interval. It is very useful when certain tasks need to be processed regularly or page elements are updated in real time. Pay attention to the following when using setInterval performance and reliability issues and optimize as needed.

You can use the clearInterval function to stop a timer created by the setInterval function. The setInterval function returns a unique timer ID, which can be passed as a parameter to the clearInterval function to stop the execution of the timer.

setTimeout(function,duration) - This function calls the function after duration milliseconds. This works for one execution. Let's see an example - it waits for 2000 milliseconds and then runs the callback function alert('Hello') - setTimeout(function(){alert('Hello');},2000); setInterval(function,uration) - this function is The function is called after every duration milliseconds. This can be done an unlimited number of times. Let's see an example - it triggers an alarm every 2000 ms
