Home > Web Front-end > JS Tutorial > body text

Implementing value transfer to a page every N seconds based on Jquery ajax technology

亚连
Release: 2018-05-24 15:18:41
Original
1533 people have browsed it

This article introduces jquery ajax technology to transfer values ​​​​to a page at regular intervals, as well as the syntax of the setinterval() method. Friends who are interested in this article can refer to it

Sometimes we It is necessary to pass values ​​to a certain page every once in a while, such as a chat room. Every few seconds, the value is passed to the database processing page and retrieved, and then displayed in the chat window. Or it can check every once in a while whether there is an interval of 2 minutes between the last time the user spoke and now, and if so, log out the user. At this time we will use the HTML DOM setInterval() method.

The setInterval() method calls a function or evaluates an expression at a specified period (in milliseconds).

The setInterval() method will continue to call the function until clearInterval() is called or the window is closed. The ID value returned by setInterval() can be used as an argument to the clearInterval() method.

Syntax:

setInterval(code,millisec[,"lang"])

code

Required. A function to be called or a string of code to be executed.

millisec

Must. The time interval, in milliseconds, between periodic executions or calls to code.

eg:

 setInterval(function(){
      host = window.location.host
      $.post("http://"+host+"/index.php/Article/cpMes/value/1");
    },5000);
Copy after login

Extension:

clearInterval() method
clearInterval() method can be canceled by setInterval () set timeout.
The parameter of the clearInterval() method must be the ID value returned by setInterval().

eg:

<html>
<body>
<input type="text" id="clock" size="35" />
<script language=javascript>
var int=self.setInterval("clock()",50)
function clock()
 {
 var t=new Date()
 document.getElementById("clock").value=t
 }
</script>
</form>
<button onclick="int=window.clearInterval(int)">
Stop interval</button>
</body>
</html>
Copy after login

The above content is an introduction to this article about Jquery ajax technology that implements value transfer to a page every N seconds. I hope you like it.

The above is what I compiled for everyone. I hope it will be helpful to everyone in the future.

Related articles:

Solving the forward and backward problem of ajax based on Jquery.history

How to solve the problem of Ajax request session failure

Writing lightweight ajax components 02--A brief analysis of AjaxPro

The above is the detailed content of Implementing value transfer to a page every N seconds based on Jquery ajax technology. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!