setInterval() 是异步函数吗?
Is the setInterval() an asynchronous function?
我每秒向服务器发送一个XMLHttpRequest
,服务器将响应新消息。为了每秒调用 XMLHttpRequest
,我在 SharedWorker
.
中使用了 setInterval()
函数
但是,由于我每秒都在发出请求,所以我想知道setInterval()
是否是异步的?
例如,如果一个 XMLHttpRequest
请求用了 3 秒完成 "due to a delay",我会同时处理 3 个请求还是 setInterval()
等到第一个请求完成在它等待 1 秒并发送另一个请求之前?
这是我的代码
function checkQueue(url)
{
var xhr = new XMLHttpRequest();
xhr.addEventListener("load", reqListener);
xhr.open('GET', url, true);
xhr.send();
}
function reqListener ()
{
var queue = JSON.parse(this.responseText);
notifyAllPorts(queue);
console.log(this.responseText);
}
setInterval(
function() {
checkQueue('/add-ons/icws/push.php')
}
, 1000);
一旦当前调用堆栈执行完毕,setInterval
只是将代码排队到 运行。这对某些事情很有用。
所以是的,它是异步的,因为它打破了同步流程,但它实际上不会执行 concurrently/on 一个单独的线程。如果您的目标是后台处理,请查看网络工作者。
因此,无论服务器花费多少时间,它都会每秒请求一次,因为您的代码设置为 1000
是的,你会 运行 惹上麻烦。 setInterval
无论您的请求状态如何,都会像发条一样响起。
你最好在每个请求完成时使用 setTimeout
启动一次性计时器...所以:
function checkQueue(url)
{
var xhr = new XMLHttpRequest();
xhr.addEventListener("load", reqListener);
xhr.open('GET', url, true);
xhr.send();
}
function reqListener ()
{
var queue = JSON.parse(this.responseText);
notifyAllPorts(queue);
console.log(this.responseText);
setTimeout(
function() {
checkQueue('/add-ons/icws/push.php')
}, 1000);
}
checkQueue('/add-ons/icws/push.php')
是的,setInterval 和 setTimeout 是异步的,但是你不做推送,你做拉,如果你想要一个推送请求阅读网络套接字
如前所述,它不会等到请求完成。这是一种间隔承诺的方法:
function checkQueue(url, cb) {
var xhr = new XMLHttpRequest();
xhr.addEventListener("loadend", cb);
xhr.addEventListener("load", reqListener);
xhr.open('GET', url, true);
xhr.send();
}
function reqListener ()
{
var queue = JSON.parse(this.responseText);
notifyAllPorts(queue);
console.log(this.responseText);
}
var promise = Promise.resolve(true);
setInterval(function () {
promise = promise.then(function () {
return new Promise(function (resolve) {
checkQueue(yourUrlHere, resolve);
});
});
}, 1000);
它会继续每秒添加请求,但如果超过 1 秒,它会自行延迟。
我每秒向服务器发送一个XMLHttpRequest
,服务器将响应新消息。为了每秒调用 XMLHttpRequest
,我在 SharedWorker
.
setInterval()
函数
但是,由于我每秒都在发出请求,所以我想知道setInterval()
是否是异步的?
例如,如果一个 XMLHttpRequest
请求用了 3 秒完成 "due to a delay",我会同时处理 3 个请求还是 setInterval()
等到第一个请求完成在它等待 1 秒并发送另一个请求之前?
这是我的代码
function checkQueue(url)
{
var xhr = new XMLHttpRequest();
xhr.addEventListener("load", reqListener);
xhr.open('GET', url, true);
xhr.send();
}
function reqListener ()
{
var queue = JSON.parse(this.responseText);
notifyAllPorts(queue);
console.log(this.responseText);
}
setInterval(
function() {
checkQueue('/add-ons/icws/push.php')
}
, 1000);
setInterval
只是将代码排队到 运行。这对某些事情很有用。
所以是的,它是异步的,因为它打破了同步流程,但它实际上不会执行 concurrently/on 一个单独的线程。如果您的目标是后台处理,请查看网络工作者。
因此,无论服务器花费多少时间,它都会每秒请求一次,因为您的代码设置为 1000
是的,你会 运行 惹上麻烦。 setInterval
无论您的请求状态如何,都会像发条一样响起。
你最好在每个请求完成时使用 setTimeout
启动一次性计时器...所以:
function checkQueue(url)
{
var xhr = new XMLHttpRequest();
xhr.addEventListener("load", reqListener);
xhr.open('GET', url, true);
xhr.send();
}
function reqListener ()
{
var queue = JSON.parse(this.responseText);
notifyAllPorts(queue);
console.log(this.responseText);
setTimeout(
function() {
checkQueue('/add-ons/icws/push.php')
}, 1000);
}
checkQueue('/add-ons/icws/push.php')
是的,setInterval 和 setTimeout 是异步的,但是你不做推送,你做拉,如果你想要一个推送请求阅读网络套接字
如前所述,它不会等到请求完成。这是一种间隔承诺的方法:
function checkQueue(url, cb) {
var xhr = new XMLHttpRequest();
xhr.addEventListener("loadend", cb);
xhr.addEventListener("load", reqListener);
xhr.open('GET', url, true);
xhr.send();
}
function reqListener ()
{
var queue = JSON.parse(this.responseText);
notifyAllPorts(queue);
console.log(this.responseText);
}
var promise = Promise.resolve(true);
setInterval(function () {
promise = promise.then(function () {
return new Promise(function (resolve) {
checkQueue(yourUrlHere, resolve);
});
});
}, 1000);
它会继续每秒添加请求,但如果超过 1 秒,它会自行延迟。