Function.createCallback doesn't pass context correctly in FireFox

杀马特。学长 韩版系。学妹 提交于 2019-12-02 13:51:31

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/969326/function-createcallback-doesnt-pass-context-correctly-in-firefox

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!