How to Set latency in JavaScript programs

Source: Internet
Author: User
In JavaScript, there are two ways to set latency (timedelay ). The first method is very simple. This method will wait for the set duration before running the function code. The second method is the same, but it will repeatedly run the function code. Note that the minimum latency of many browsers is between 25 to 75 milliseconds, and the minimum latency of some extreme speed browsers can reach 3.

In JavaScript, there are two ways to set latency (timedelay ). The first method is very simple. This method will wait for the set duration before running the function code. The second method is the same, but it will repeatedly run the function code.

Note that the minimum latency of many browsers is between 25 and 75 milliseconds, and the minimum latency of some speed browsers can reach 3 milliseconds. If the latency value is smaller than the minimum value of the browser, the actual latency of code execution is the minimum latency value of the browser. Even if the set latency value is higher than the minimum value, the actual latency is not perfect. The actual latency of most browsers is slightly higher than the set value. Generally, the latency is only several milliseconds. Some browsers will fix the error in the interval timer (intervaltimer. In addition, setting too many timers on the same page causes the browser to slow down or even have no response. Three or four timers are generally acceptable limits.

SetTimeout

The first method is to use the setTimeout method of the Window object. This method sets a specific millisecond value and then runs the Specified Code. The Code can be a direct reference to the function, or a string as the source code.

Window. setTimeout (referenceToFunction, timeInMilliseconds); window. setTimeout ('runmorecode () ', timeInMilliseconds );

If possible, you should use direct function reference to make it more effective. If the string is used, the browser is required to create a new script environment to run the script.

If you create a timer, the code after the timer runs as usual. After the delay is set (the old thread ends), the timer starts a new thread. The Code specified by setTimeout will run in the new thread, while other code will still run in the original thread. Compared with other complex languages, JavaScript does not provide any way to control thread sleep, wake up, or generate. The JavaScript engine will handle everything and the new thread will be executed after the current thread ends. Although theoretically, the multi-thread scripting engine can execute new threads while the old threads are running, the JavaScript engine is a single-thread reference program that only allows the end of a thread, enable other threads. This rule also applies to events that run in their own threads and can be triggered at any time. It waits until the current thread ends before enabling the new thread.

You can use either of the following methods to pass parameters to a latency Timer. Use a string, but make sure that the parameter you pass can be represented by the basic data type. Do not contain any characters (such as quotation marks) that can break the string format ). If yes, avoid using this method. But in case you need to use this method, the following provides an example reference.

Window. setTimeout ('runmorecode (''+ someString +'', '+ someNumber +') ', 10 );

It is more convenient to directly use functions, because you can pass variables as additional parameters to the setTimeout method. In addition to being more convenient, This method also accepts any type of variables. This should be noted that this method does not work in IE. Be careful with browsers using the pivoillagecko engine (such as Firefox and Netscape6 +), because these browsers always transmit an additional parameter to the function-number of milliseconds (number of milisecondserror ), the following example uses an inline anonymous function, which is equivalent to a direct function reference.

Window. setTimeout (function (a, B) {// do something with a and B}, 10, someString, someObject );

The variable transferred to the function is the value saved by the variable when the function is called. For example, if you have a variable named myVar, the saved value is 5. If you call the latency function, pass the variable myVar to it, and set a latency value of 1 second. Then you immediately change the value saved by myVar to 7. When the latency function is run, it runs the function referenced by the latency function and passes the number 5 to the referenced function, because when the latency function is called, the value of the variable myVar is 5.

The tragedy is that IE cannot directly pass additional parameters to the latency function. A common way to ensure compatibility is to ensure that the variables are in the current scope so that the latency function can be directly referenced.

SetInterval

The setInterval method is syntactically the same as setTimeout. The difference is that it will run repeatedly at the same interval of latency until it is canceled.

Window. setInterval (function (a, B) {// do something with a and B}, 10, someString, someObject );

Clear timeout and interval timers

You can call the corresponding clearTimeout and clearInterval methods to cancel the timeout and interval timers, and the code in the timer will not run. In this example, the interval timer is set to run once every second. The timeout timer is set to cancel the interval timer after 3.5 seconds.

Var myInterval = window. setInterval (function (a, B) {myNumber ++;}, 1000); window. setTimeout (function (a, B) {clearInterval (myInterval) ;},3500 );

Obtaining a timeout object is the same as obtaining an inteval object. You can also use clearTimeout to cancel the timeout timer.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.