Javascript 每 4 秒重复一次代码

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6403075/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 21:34:00  来源:igfitidea点击:

Repeat code every 4 seconds

javascriptjquery

提问by Gabriel Uhlí?

I want repeat this code every 4 seconds, how i can do it with javascript or jquery easly ? Thanks. :)

我想每 4 秒重复一次这段代码,我怎么能用 javascript 或 jquery 轻松地做到这一点?谢谢。:)

$.get("request2.php", function(vystup){
   if (vystup !== ""){
      $("#prompt").html(vystup);
      $("#prompt").animate({"top": "+=25px"}, 500).delay(2000).animate({"top": "-=25px"}, 500).delay(500).html("");
    }
});

采纳答案by user113716

Another possibility is to use setTimeout, but place it along with your code in a function that gets called recursively in the callback to the $.get()request.

另一种可能性是使用setTimeout,但将它与您的代码一起放在一个函数中,该函数在$.get()请求的回调中被递归调用。

This will ensure that the requests are a minimumof 4 seconds apart since the next request will not begin until the previous response was received.

这将确保请求至少相隔 4 秒,因为在收到前一个响应之前下一个请求不会开始。

 // v--------place your code in a function
function get_request() {
    $.get("request2.php", function(vystup){
       if (vystup !== ""){
          $("#prompt").html(vystup)
                      .animate({"top": "+=25px"}, 500)
                      .delay(2000)
                      .animate({"top": "-=25px"}, 500)
                      .delay(500)
                      .html("");
        }
        setTimeout( get_request, 4000 ); // <-- when you ge a response, call it
                                         //        again after a 4 second delay
    });
}

get_request();  // <-- start it off

回答by BrunoLM

Use setIntervalfunction

使用setInterval函数

setInterval( fn , miliseconds )

From MDC docs:

来自 MDC 文档:

Summary

Calls a function repeatedly, with a fixed time delay between each call to that function.

Syntax

概括

重复调用一个函数,在每次调用该函数之间有一个固定的时间延迟。

句法

var intervalID = window.setInterval(func, delay[, param1, param2, ...]);
var intervalID = window.setInterval(code, delay);

where

intervalIDis a unique interval ID you can pass to clearInterval().

funcis the function you want to be called repeatedly.

codein the alternate syntax, is a string of code you want to be executed repeatedly. (Using this syntax is not recommended for the same reasons as using eval())

delayis the number of milliseconds (thousandths of a second) that the setInterval() function should wait before each call to func. As with setTimeout, there is a minimum delay enforced.

Note that passing additional parameters to the function in the first syntax does not work in Internet Explorer.

在哪里

intervalID是可以传递给 clearInterval() 的唯一间隔 ID。

func是您要重复调用的函数。

代码的替代语法,是一串代码,你要重复执行。(出于与使用 eval() 相同的原因,不建议使用此语法)

delay是 setInterval() 函数在每次调用 func 之前应该等待的毫秒数(千分之一秒)。与 setTimeout 一样,强制执行最小延迟。

请注意,将附加参数传递给第一种语法中的函数在 Internet Explorer 中不起作用。

Example

例子

// alerts "Hey" every second
setInterval(function() { alert("Hey"); }, 1000);

回答by gblazex

setInterval(function(){
  // your code...
}, 4000);

回答by Jacob

It's not too hard in javascript.

在 javascript 中并不太难。

// declare your variable for the setInterval so that you can clear it later
var myInterval; 

// set your interval
myInterval = setInterval(whichFunction,4000);

whichFunction{
    // function code goes here
}

// this code clears your interval (myInterval)
window.clearInterval(myInterval); 

Hope this helps!

希望这可以帮助!

回答by jasonleonhard

const milliseconds = 1000 

setInterval(
  () => { 
  // self executing repeated code below

}, milliseconds);