发布时间:2025-12-11 01:17:38 浏览次数:1
jQuery的setInterval方法用于重复执行一个函数或一段代码,可以指定执行的时间间隔。
基本语法如下:
setInterval(function, milliseconds);其中,function为要执行的函数或代码块,milliseconds为执行的时间间隔,以毫秒为单位。
例如,以下代码每隔1秒输出一次"Hello World!":
setInterval(function() {console.log("Hello World!");}, 1000);注意,setInterval方法会返回一个唯一的标识符,可以使用clearInterval方法来停止定时器的执行。例如:
var intervalId = setInterval(function() {console.log("Hello World!");}, 1000);// 5秒后停止定时器的执行setTimeout(function() {clearInterval(intervalId);}, 5000);