JavaScript uses the setInterval () function to implement simple round-robin operations. setinterval round-robin
This example describes how JavaScript uses the setInterval () function to perform simple round robin. Share it with you for your reference. The specific analysis is as follows:
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 simple round-robin operations. You can read the content in the text box 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:
Copy codeThe Code is as follows: <! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<Html xmlns = "http://www.w3.org/1999/xhtml">
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8"/>
<Title> Polling </title>
<Script type = "text/javascript">
Function synchronous (){
Document. getElementById ("ptext"). innerHTML = document. getElementById ("text"). value;
}
Function Polling (){
Synchronous ();
SetInterval ("synchronous ()", 500 );
}
</Script>
</Head>
<Body onLoad = "Polling ()">
<Input type = "text" id = "text"/>
<P id = "ptext"> </p>
</Body>
</Html>
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.
I hope this article will help you design javascript programs.