简体   繁体   English

一段时间后自动调用Javascript函数的正确方法是什么?

[英]what is the proper way to auto call function in Javascript after an interval of time?

I need to call a function after one minute and update the div . 一分钟后,我需要调用一个函数并更新div

Basically I need to refresh the page as the new entry comes in the table, so what I think is to Ajax call and update the content after one minute. 基本上,当表中出现新条目时,我需要刷新页面,所以我认为是Ajax调用并在一分钟后更新内容。 Please tell me whether this is right approach? 请告诉我这是正确的方法吗?

If you need the function to run every 60 seconds, you can use setInterval() : 如果您需要每60秒运行一次该函数,则可以使用setInterval()

setInterval(function() {
    /* AJAX call here. */
}, 60000);

If you only need it to run only once, you can use setTimeout() : 如果只需要运行一次,则可以使用setTimeout()

setTimeout(function() {
    /* AJAX call here. */
}, 60000);

For both, 60,000 is the millisecond delay (60,000ms = 1 minute). 对于这两者,60,000是毫秒延迟(60,000ms = 1分钟)。

Instead of using a setInterval you could execute a setTimeout of 60 seconds (60000 ms) to the function that makes the ajax call only if the previous call has been successfully completed (so, as example, you can avoid to make useless ajax calls if your server return a 404 or a 500 status for a given resource) 除了可以使用setInterval还可以对仅在成功完成上一个调用后才进行ajax调用的函数执行60秒(60000 ms)的setTimeout (因此,例如,如果您的Ajax调用服务器返回给定资源的404或500状态)

var xhr = new XMLHttpRequest();

function doAjaxCall() {
    ...
    xhr.onReadyStateChange = function() {

        ...
        if (xhr.readyState === 4) {
            if (xhr.status === 200 || xhr.status === 304) { 
                setTimeout(doAjaxCall, 60000);
            }
        }
    }
}

doAjaxCall();

setTimeout() and it's brother setInterval() setTimeout()和它的兄弟setInterval()

setTimeout - calls once setTimeout-调用一次

setInterval - calls every specified interval setInterval-每个指定间隔调用一次

Examples: 例子:

setTimeout(function() { doStuffOnce(); }, 1000);
setInterval(function() { doStuff(); }, 1000);

If all you need to do is wait for the AJAX to complete, then you shouldn't use a fixed timeout. 如果您只需要等待AJAX​​完成,那么您就不应该使用固定的超时时间。 What happens if the AJAX call takes more than one minute? 如果AJAX通话时间超过一分钟怎么办? And why make users wait for a whole minute (which is forever in a browser) if the AJAX call completes (as is typical) in a second or so. 以及如果AJAX调用在大约一秒钟内完成(通常如此),为什么让用户等待一整分钟(这在浏览器中永远如此)。 Instead, simply use the callback of the AJAX call itself, eg 取而代之,只需使用AJAX调用本身的回调即可,例如

$("/url/to/your/AJAX").done(function(data) {
  // do what you need to do with the data now
})

Youy can use both The setTimeout() method calls a function or evaluates an expression after a specified number of milliseconds. 您可以同时使用setTimeout()方法来调用函数或在指定的毫秒数后求值表达式。

setTimeout(code,millisec,lang) setTimeout(code,millisec,lang)

code Required. 代码必填。 The function that will be executed millisec Required. 将执行的功能millisec必需。 The number of milliseconds to wait before executing the code lang Optional. 执行代码lang之前要等待的毫秒数Optional。 The scripting language: JScript | 脚本语言:JScript | VBScript | VBScript | JavaScript 的JavaScript

The setInterval() method calls a function or evaluates an expression at specified intervals (in milliseconds). setInterval()方法以指定的时间间隔(以毫秒为单位)调用函数或计算表达式。

The setInterval() method will continue calling the function until clearInterval() is called, or the window is closed. setInterval()方法将继续调用该函数,直到调用clearInterval()或关闭窗口为止。

The ID value returned by setInterval() is used as the parameter for the clearInterval() method. setInterval()返回的ID值用作clearInterval()方法的参数。

Tip: 1000 ms = 1 second. 提示:1000毫秒= 1秒。

setInterval(code,millisec,lang) setInterval(code,millisec,lang)

code Required. 代码必填。 The function that will be executed millisec Required. 将执行的功能millisec必需。 The intervals (in milliseconds) on how often to execute the code lang Optional. 关于执行代码的频率的间隔(以毫秒为单位) lang可选。 JScript | JScript | VBScript | VBScript | JavaScript 的JavaScript

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM