This example describes the way JavaScript uses the SetInterval () function to implement a simple polling operation. Share to everyone for your reference. The specific analysis is as follows:
Polling (Polling) is a way for CPU decisions to provide peripheral device services, also known as "programmed output" (programmed I/O). The concept of the polling method is that the CPU periodically issue inquiries, sequentially ask each peripheral equipment whether it needs its services, there is to give services, after the end of the service to ask the next perimeter, and then constantly cycle. The polling method is easy to implement, but the efficiency is low.
In JavaScript using the SetInterval function for simple polling operations, you can always determine a parameter value, but do not need to refresh the page, that is, do not have to add <meta http-equiv= "Refresh" content= "5" in the header > Make a decision to refresh the page.
I. BASIC OBJECTIVES
As an input box, without the onchange () function, use the SetInterval function directly for simple polling operations, every 0.5 seconds to read the text box content
In fact, the same principle as the JavaScript clock, every second to take the current time, and then update the text content
Second, the production process
The code is as follows, no longer repeating:
Copy Code code as follows:
<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
<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>
<body onload= "Polling ()" >
<input type= "text" id= "text"/>
<p id= "Ptext" ></p>
</body>
The polling () function starts execution after the page is loaded, executes the synchronous () function, and always executes the synchronous every 0.5 seconds (); function
I hope this article will help you with your JavaScript programming.