Home > Web Front-end > Front-end Q&A > How to set a few seconds loop in jquery

How to set a few seconds loop in jquery

WBOY
Release: 2022-01-10 17:36:58
Original
1554 people have browsed it

In jquery, you can use the setInterval() method to set the specified code for a few seconds loop. This method can call functions or calculate expressions according to the specified period in milliseconds until clearInterval() is called or the window is closed, the syntax is "setInterval(call function, time)".

How to set a few seconds loop in jquery

The operating environment of this tutorial: windows10 system, jquery3.2.1 version, Dell G3 computer.

How to set a few seconds loop in jquery

The setInterval() method can call a function or calculate an expression according to the specified period (in milliseconds) Mode.

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

Parameter Description

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.

Return value

A value that can be passed to Window.clearInterval() to cancel the periodic execution of code.

Example

<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

Output result:

How to set a few seconds loop in jquery

Related video tutorial recommendation: jQuery video tutorial

The above is the detailed content of How to set a few seconds loop in jquery. 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