简体   繁体   English

定期运行JavaScript函数

[英]Run JavaScript function at regular time interval

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. 我想要添加到项目页面的是一个循环播放屏幕截图的幻灯片,大约每5秒更改一次图像。 Is there any way to a script triggered at a time interval using just JavaScript? 有什么办法可以仅使用JavaScript在一定时间间隔内触发脚本吗? ..or will I have to resort to alternative methods for achieving my desired functionality. ..或者我将不得不采用替代方法来实现所需的功能。 Thanks in advance for any help! 在此先感谢您的帮助!

setInterval : setInterval

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

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

Pass it the function you want called repeatedly every n milliseconds. 每隔n毫秒将您要重复调用的函数传递给它。 ( setTimeout , by the way, will call a function with a timeout.) (顺便说一下, setTimeout将调用带有超时的函数。)

If you ever want to stop the timer, hold onto setInterval 's return value and pass it to clearInterval . 如果您想停止计时器,请保留setInterval的返回值并将其传递给clearInterval

You want the setInterval function. 您需要setInterval函数。

setInterval(function() {
  // This will be executed every 5 seconds
}, 5000); // 5000 milliseconds

Basic reference: http://www.w3schools.com/jsref/met_win_setinterval.asp (please ignore the reference to the "lang" parameter) 基本参考: http : //www.w3schools.com/jsref/met_win_setinterval.asp (请忽略对“ lang”参数的参考)

More indepth reference: https://developer.mozilla.org/en-US/docs/Web/API/window.setInterval 更深入的参考: https : //developer.mozilla.org/en-US/docs/Web/API/window.setInterval

You can use window.setInterval 您可以使用window.setInterval

Sample usage: 用法示例:

window.setInterval(function () {
    console.log("foo");
}, 3000);

It Changes the date time in a div and time changes frequently after 1 sec. 它会更改div中的日期时间,并且1秒后会频繁更改时间。

    setInterval(function(){
      var date=new Date();
      $('.class').html(date);
    },1000);
<script>
var intervalVariable=setInterval(function(){
   //Your operation goes Here
  },1000); // executes every 1000 milliseconds(i.e 1 sec)

function stopTimer()
{
   clearInterval(intervalVariable);
}
</script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 以时间间隔连续重复运行JavaScript函数 - Run a JavaScript function continuously repeating with a time interval 创建一个时间间隔,该时间间隔将导致函数在JavaScript中随机运行 - Create a time interval that will cause a function to run at a random time in JavaScript Javascript 运行 function 一次在另一个 function 设置为间隔 - Javascript run function one time in another function which sets as interval javascript执行时间间隔的函数 - javascript execute function with time interval 具有时间间隔的Java中的函数调用助手函数 - Call A Helper Function to a function in Javascript with Time Interval Javascript While或If语句与 - Javascript While or If statement to run a set interval function with 我试图让 JavaScript function 在特定时间间隔内每 50 毫秒运行一次,但它不起作用 - I am trying to make a JavaScript function run every 50ms for a certain time interval, but it's not working 设置JavaScript函数的时间间隔(以“微秒”为单位) - set time interval for javascript function in “microseconds” 时间间隔秒数在使用制表符更改的Javascript上快速运行 - Time Interval seconds run fast on tab changed Javascript 使用javascript定期间隔时间运行php脚本 - run php script periodically with interval time using javascript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM