The reason you don’t have access to the main variables of the function in the callback is because the code executed by setTimeout() is executed in a separate execution context for the function from which it was called. Simply put, this not the same in callback.
If you do not want to use the Javascript libraries that provide functions for binding the callback to the context (see jQuery.proxy and underscore.bind ), you will need to write your own non-native setTimeout function to fix this.
Here is the solution suggested in the web api docs (see The "this" problem section):
window.setTimeout = function (vCallback, nDelay /*, argumentToPass1, argumentToPass2, etc. */) { var oThis = this, aArgs = Array.prototype.slice.call(arguments, 2); return __nativeST__(vCallback instanceof Function ? function () { vCallback.apply(oThis, aArgs); } : vCallback, nDelay); };
If you don't want to go this far, improving your code would be to pass arguments to the callback as parameters to setTimeout :
var timeoutID = window.setTimeout(func, delay, [param1, param2, ...]);
Read the JavaScript web api for more details.
So, in your example, you can simply write something like:
setTimeout(function (x) { debugger; }, 100, newValue);