问题描述
我发现了MS AJAX库如何与FireFox交互,似乎是一个错误 - 但也许我只是做错了。我有一个看起来像这样的脚本:
dowork({value:some value,currentRetry:0} );
//尝试连接至少10次,在第二次重试之间。
function dowork(request){
if(request.currentRetry< 10) {
logMessage('currentRetry ='+ request.currentRetry +'; 1秒后再次尝试。
request.currentRetry ++;
var callback = Function.createCallback(dowork,{value:request.context,currentRetry:request.currentRetry});
setTimeout(callback,1000);
}
else {
logMessage('Exceeded retries; currentRetry ='+ request.currentRetry);
}
}
换句话说,我想做某事这可能会定期失败,所以我想重试,说,10次,与之间的第二个。我可以弄清楚如何做到这一点的唯一方法是使用像AJAX库中的Function.createCallback位。
IE 8和Chrome 2,即它产生以下输出:
currentRetry = 0;
currentRetry = 1;
currentRetry = 2;
currentRetry = 3;在1秒后再次尝试。
currentRetry = 4;在1秒后再次尝试。
currentRetry = 5;
currentRetry = 6;
currentRetry = 7;
currentRetry = 8;
currentRetry = 9;
超过重试次数。然而,在FireFox(3.5预览,没有在其他口味测试),输出如下:
currentRetry = 0;在1秒内再次尝试。
超过重试次数; currentRetry = undefined
在解决方法或我做错了什么想法?
好吧,我不知道与Function.createCallback有什么问题,但我能够通过使用匿名方法来修复它:
var callback = function(){dowork(request)};
setTimeout(callback,1000);
足够接近政府工作。
I've discovered what seems to be a bug in how the MS AJAX library interacts with FireFox -- but maybe I'm just doing it wrong. I've got a script that looks something like this:
dowork({ value: "some value", currentRetry: 0 });
// Try to connect at least 10 times, with a second in-between retries..
function dowork(request) {
if (request.currentRetry < 10) {
logMessage('currentRetry = ' + request.currentRetry + '; trying again in 1 second.');
request.currentRetry++;
var callback = Function.createCallback(dowork, { value: request.context, currentRetry: request.currentRetry });
setTimeout(callback, 1000);
}
else {
logMessage('Exceeded retries; currentRetry = ' + request.currentRetry);
}
}
In other words, I'm trying to do something that's likely to fail periodically, so I want to retry, say, 10 times, with a second in-between. The only way I can figure out how to do this is by using something like the Function.createCallback bit from the MS Ajax library.
And this works correctly in, say, IE 8 and Chrome 2, i.e., it produces the following output:
currentRetry = 0; trying again in 1 second.
currentRetry = 1; trying again in 1 second.
currentRetry = 2; trying again in 1 second.
currentRetry = 3; trying again in 1 second.
currentRetry = 4; trying again in 1 second.
currentRetry = 5; trying again in 1 second.
currentRetry = 6; trying again in 1 second.
currentRetry = 7; trying again in 1 second.
currentRetry = 8; trying again in 1 second.
currentRetry = 9; trying again in 1 second.
Exceeded retries; currentRetry = 10
However, in FireFox (3.5 Preview, haven't tested it in other flavors), the output looks like this:
currentRetry = 0; trying again in 1 second.Exceeded retries; currentRetry = undefined
Any thoughts either on a workaround, or on what I'm doing wrong?
Well, I don't know what the problem is with Function.createCallback, but I was able to fix it by using an anonymous method instead:
var callback = function () { dowork(request) };
setTimeout(callback, 1000);
Close enough for government work.
这篇关于Function.createCallback在FireFox中没有正确传递上下文的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!