user263097
user263097

Reputation: 294

Javascript setTimeout

Can you tell me why this works:

PageMethods.UpdateForcedDisposition(forcedDisposition, a.value, SucceededCallback, FailedCallback);

When this doesn’t?

setTimeout("PageMethods.UpdateForcedDisposition(" + forcedDisposition + "," + a.value + ", SucceededCallback, FailedCallback);", 1000);

Interestingly, a similar call works with setTimeout:

setTimeout("PageMethods.UpdateSales(" + id + ", " + a.value + ", SucceededCallback, FailedCallback);", 1000);

…I’m stumped!

Upvotes: 1

Views: 649

Answers (1)

Andy E
Andy E

Reputation: 344575

Avoid passing a string to setTimeout. Where possible, use anonymous functions:

window.setTimeout(function () {
    PageMethods.UpdateForcedDisposition(
        forcedDisposition, 
        a.value, 
        SucceededCallback, 
        FailedCallback
    );
}, 1000);

A setTimeout with a string executes in the global scope. If you're trying to reference variables from the current scope, you'll hit an error.

Upvotes: 6

Related Questions