[JavaScript] uses the setInterval () function for simple round robin. setinterval round robin

Source: Internet
Author: User

[JavaScript] uses the setInterval () function for simple round robin. setinterval round robin

Polling is a way for the CPU to decide how to provide peripheral device services, also known as Programmed I/O ). The concept of the Round-Robin method is that the CPU regularly sends a query, asking each peripheral device in sequence whether or not they need the service. Once the service is completed, it then asks the next peripheral device and continues to repeat it. The polling method is easy to implement, but inefficient.

When using the setInterval function in JavaScript For simple round robin operations, you can determine a parameter value at any time, but you do not need to refresh the page, that is, you do not need to add <META HTTP-EQUIV = "Refresh" CONTENT = "5"> in the page header to Refresh the page.


I. Basic Objectives

You do not need the onChange () function to directly use the setInterval function for a simple round-robin operation. The content in the text box is not read every 0.5 seconds.


In fact, the principle is the same as that of the JavaScript clock. It takes the current time every second and then updates the text content once.


Ii. Production Process

The Code is as follows:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">The polling () function starts execution after the page is loaded. the synchronous () function is executed first, and then the synchronous () function is always executed every 0.5 seconds.


JAVASCRIPT setInterval operation

<Html>
<Body>
<Div id = "one"> 5 </div>
<Script>
Var I = 5;
Var one = document. getElementById ("one ");
Var si = setInterval (function (){
One. innerText = I --;
If (I = 0) clearInterval (si );
}, 1000)
</Script>
</Body>
</Html>

How does javascript clear setInterval in another function?

// The first floor and second floor are completely incorrect. In the timer call, it is show (). You have not declared this function and generate an incorrect timer. The code must have an error.

Var timer1 = null;

Function start (){
If (timer1)
Return;
Timer1 = setInterval ("test ()", 200 );
}
Function end (){
If (timer1)
{
ClearInterval (timer1 );
Timer1 = null;
}
}

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.