Function.createCallback doesn't pass context c

2019-03-01 00:43发布

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条回答
爷的心禁止访问
2楼-- · 2019-03-01 01:17

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.

查看更多
登录 后发表回答