以固定的时间间隔运行JavaScript函数

编程入门 行业动态 更新时间:2024-10-27 10:21:47
本文介绍了以固定的时间间隔运行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函数

本文发布于:2023-10-16 11:27:50,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1497425.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:间隔   函数   时间   JavaScript

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!