以固定的时间间隔运行JavaScript函数 [英] Run JavaScript function at regular time interval

查看:170
本文介绍了以固定的时间间隔运行JavaScript函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在建立一个托管软件的网站。我想要的是添加到项目页面的是一个循环截图的幻灯片,每5秒更换一次图像。是否有任何方法可以使用JavaScript在一个时间间隔触发脚本? ..或者我将不得不采用其他方法来实现我所需的功能。
提前感谢您的帮助!

I am currently building a website to host software. What I want is to add to the project pages is a slideshow of screenshots cycling, changing images about every 5 seconds. Is there any way to a script triggered at a time interval using just JavaScript? ..or will I have to resort to alternative methods for achieving my desired functionality. Thanks in advance for any help!

推荐答案

setInterval

function doSomething() {
    alert('This pops up every 5 seconds and is annoying!');
}

setInterval(doSomething, 5000); // Time in milliseconds

每隔 n 重复传递你想要调用的函数>毫秒。 ( setTimeout ,顺便说一下,会调用一个超时的函数。)

Pass it the function you want called repeatedly every n milliseconds. (setTimeout, by the way, will call a function with a timeout.)

如果你想要停止计时器,保持 setInterval 的返回值并将其传递给 clearInterval

If you ever want to stop the timer, hold onto setInterval’s return value and pass it to clearInterval.

这篇关于以固定的时间间隔运行JavaScript函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆