gpt4 book ai didi

javascript - 试图让 knockout js 检查数据是否每 10 秒来一次

转载 作者:行者123 更新时间:2023-11-29 19:10:38 25 4
gpt4 key购买 nike

我正在尝试调用发布 ajax 请求的函数来检查数据库,每 10 秒查看我想要的所需信息是否已到达,如果所需信息在 60 秒内未到达,我只想退出功能。我现在遇到的问题是,我的函数没有每 10 秒调用一次 self.startInstantorCheck,然后在 60 秒后退出到最后。这是函数

self.startInstatorCheck = function(){
self.instatorStarts(true);
var MAX_WAIT_TIME_MS = 1 * 60 * 1000;
var POST_INTERVAL_MS = 10 * 1000;
var timeoutTime = null;
$.ajax({
type: 'POST',
url: BASEURL + 'index.php/moneyexchange/check_instantor_field/' + auth,
contentType: 'application/json; charset=utf-8'
})
.done(function(userinfo) {
if (timeoutTime == null) {
// Start of the timeout cycle:
timeoutTime = Date.now() + MAX_WAIT_TIME_MS;
}
if (userinfo.instantor_request > 12) {
self.allInstantorCheckMessages('Instantor data gathered');
} else {
if (Date.now() < MAX_WAIT_TIME_MS) {
setTimeout(self.startInstatorCheck(), POST_INTERVAL_MS);
} else {
self.allInstantorCheckMessages('Please go through instantor to ');
self.instatorStarts(true);
self.magicInstantorbtn2(true);
}
}
})
.fail(function(jqXHR, textStatus, errorThrown) {
self.errorMessage(errorThrown);
})
.always(function(data){
});
}
I am using knockout js, so would be great if the answers or help are related to knockout js.

最佳答案

首先,这里简化了您的代码以减少嵌套并通过命名提供清晰度。

           self.startInstatorCheck = function(){
self.instatorStarts(true);
var MAX_WAIT_TIME_MS = 1 * 60 * 1000;
var POST_INTERVAL_MS = 10 * 1000;
var timeoutTime = null;
$.ajax({
type: 'POST',
url: BASEURL + 'index.php/moneyexchange/check_instantor_field/' + auth,
contentType: 'application/json; charset=utf-8'
})
.done(function(userinfo) {
// please use === not ==
// == doesn't always do what you want it to do, NEVER use it
timeoutTime = timeoutTime === null ?
Date.now() + MAX_WAIT_TIME_MS :
timeoutTime;

var pleaseNameMe = userinfo.instantor_request > 12
if (pleaseNameMe) {
return self.allInstantorCheckMessages('Instantor data gathered');
}

var expired = Date.now() < MAX_WAIT_TIME_MS
if (!expired) {
return setTimeout(self.startInstatorCheck(), POST_INTERVAL_MS);
}

self.allInstantorCheckMessages('Please go through instantor to ');
self.instatorStarts(true);
self.magicInstantorbtn2(true);

})
.fail(function(jqXHR, textStatus, errorThrown) {
self.errorMessage(errorThrown);
})
.always(function(data){
});
}

其次,你的问题之前已经解决了,knockoutjs与否。 Here is a wonderful article这解释了如何使用超时在 javascript 中进行轮询。它建议使用 promise 。我非常同意。 (它们可以而且应该与 knockout js 结合使用。)直接来自博客的人为示例:

// The polling function
function poll(fn, timeout, interval) {
var dfd = new Deferred();
var endTime = Number(new Date()) + (timeout || 2000);
interval = interval || 100;

(function p() {
// If the condition is met, we're done!
if(fn()) {
dfd.resolve();
}
// If the condition isn't met but the timeout hasn't elapsed, go again
else if (Number(new Date()) < endTime) {
setTimeout(p, interval);
}
// Didn't match and too much time, reject!
else {
dfd.reject(new Error('timed out for ' + fn + ': ' + arguments));
}
})();

return dfd.promise;
}

// Usage: ensure element is visible
poll(function() {
return document.getElementById('lightbox').offsetWidth > 0;
}, 2000, 150);

关于javascript - 试图让 knockout js 检查数据是否每 10 秒来一次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39110018/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com