JavaScript uses the SetInterval () function to implement a simple polling operation method _javascript tips

Source: Internet
Author: User
Tags setinterval

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.

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.