setTimeout 不起作用
setTimeout doesn’t work
我正在使用 Ember-Simple-Auth 来根据远程服务器验证我的应用程序,远程服务器为我提供 API 令牌,必须通过使用更新令牌每 30 分钟刷新一次.为此,我包含了对 ember-Simple-Auth 恢复方法的异步调用,该方法实现了令牌刷新,应该使用 setTimeout 每 25 分钟触发一次。然而,在对 setTimeout 的三个调用中,只有一个正常工作,尽管对我来说它们看起来都是一样的。你能不能在这件事上帮助我?
示例代码:
export default Base.extend({
restore(data) {
console.log("restoring");
var renewDelay = 30000; //set to 30 seconds for testing purpose
var expirates = data.expirates * 1000; //server response lists expiration as a unix timestamp in seconds
var current = new Date().getTime();
if (expirates < current) {
console.log("token expired " + expirates + " " + current);
return Ember.RSVP.reject();
} else {
if (expirates - current > renewDelay / 4) {
console.log("token valid " + expirates + " " + current + ", renew in " + Number(expirates - current - 30000));
//this one works as expected after 30s
setTimeout(this.restore, expirates - current - renewDelay, data);
return Ember.RSVP.resolve(data);
} else {
console.log("token valid " + expirates + " " + current + ", renewing now");
return Ember.$.ajax({
method: "GET",
url: "/renew",
accepts: 'application/json',
headers: {
Authorization: data.renew_token
}
})
.then(function(data) {
console.log("next renew in " + Number((data.expirates * 1000) - current - renewDelay));
//this one is never called
setTimeout(this.restore, (data.expirates * 1000) - current- renewDelay, data);
return data;
});
},
},
authenticate(identification, password) {
console.log("authenticating");
return Ember.$.ajax({
method: "POST",
url: "/auth",
contentType: 'application/json',
accepts: 'application/json',
data: JSON.stringify({ username: identification, password: password })
})
.then(function(data) {
console.log("next renew in " + Number((data.expirates * 1000) - new Date().getTime() - 30000));
//this one is not triggered either
setTimeout(this.restore, (data.expirates * 1000) - new Date().getTime() - 30000, data);
return data;
});
}
});
尝试简化您的代码。
试试这个:
var timeOutDelay = expirates - current- renewDelay;
setTimeout(restore, timeOutDelay, data);
您在两个回调中的 this.restore 可能引用了错误的 this,导致永远不会调用恢复函数。您是否尝试过调试 this 所指的内容?使用箭头回调可以解决这个问题,比如
.then((data) => {
...
setTimeout(this.restore, (data.expirates * 1000) - new Date().getTime() - 30000, data);
});
我正在使用 Ember-Simple-Auth 来根据远程服务器验证我的应用程序,远程服务器为我提供 API 令牌,必须通过使用更新令牌每 30 分钟刷新一次.为此,我包含了对 ember-Simple-Auth 恢复方法的异步调用,该方法实现了令牌刷新,应该使用 setTimeout 每 25 分钟触发一次。然而,在对 setTimeout 的三个调用中,只有一个正常工作,尽管对我来说它们看起来都是一样的。你能不能在这件事上帮助我?
示例代码:
export default Base.extend({
restore(data) {
console.log("restoring");
var renewDelay = 30000; //set to 30 seconds for testing purpose
var expirates = data.expirates * 1000; //server response lists expiration as a unix timestamp in seconds
var current = new Date().getTime();
if (expirates < current) {
console.log("token expired " + expirates + " " + current);
return Ember.RSVP.reject();
} else {
if (expirates - current > renewDelay / 4) {
console.log("token valid " + expirates + " " + current + ", renew in " + Number(expirates - current - 30000));
//this one works as expected after 30s
setTimeout(this.restore, expirates - current - renewDelay, data);
return Ember.RSVP.resolve(data);
} else {
console.log("token valid " + expirates + " " + current + ", renewing now");
return Ember.$.ajax({
method: "GET",
url: "/renew",
accepts: 'application/json',
headers: {
Authorization: data.renew_token
}
})
.then(function(data) {
console.log("next renew in " + Number((data.expirates * 1000) - current - renewDelay));
//this one is never called
setTimeout(this.restore, (data.expirates * 1000) - current- renewDelay, data);
return data;
});
},
},
authenticate(identification, password) {
console.log("authenticating");
return Ember.$.ajax({
method: "POST",
url: "/auth",
contentType: 'application/json',
accepts: 'application/json',
data: JSON.stringify({ username: identification, password: password })
})
.then(function(data) {
console.log("next renew in " + Number((data.expirates * 1000) - new Date().getTime() - 30000));
//this one is not triggered either
setTimeout(this.restore, (data.expirates * 1000) - new Date().getTime() - 30000, data);
return data;
});
}
});
尝试简化您的代码。
试试这个:
var timeOutDelay = expirates - current- renewDelay;
setTimeout(restore, timeOutDelay, data);
您在两个回调中的 this.restore 可能引用了错误的 this,导致永远不会调用恢复函数。您是否尝试过调试 this 所指的内容?使用箭头回调可以解决这个问题,比如
.then((data) => {
...
setTimeout(this.restore, (data.expirates * 1000) - new Date().getTime() - 30000, data);
});