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

Jquery ajax technology realizes transmitting values ​​to a certain page every N seconds

不言
Release: 2018-07-02 14:30:37
Original
1977 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"])
Copy after login

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 cancels the timeout set by setInterval().
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 is the entire content of this article. I hope it will be helpful to everyone’s study. More For more related content, please pay attention to the PHP Chinese website!

Related recommendations:

jQuery implements the method of monitoring all ajax requests on the page

Implements global validation under bootstrapValidator based on jQuery

The above is the detailed content of Jquery ajax technology realizes transmitting values ​​to a certain page every N seconds. For more information, please follow other related articles on the PHP Chinese website!

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