On 7/21/06, Eric Anderson <[EMAIL PROTECTED]> wrote:
Thomas Fuchs wrote:
> AFAIK the setTimeout makes sure that the eval() is executed in the
> window-global context,
> and not the local execution context of the method it happens to be in.

Won't it still execute in the local context because of the closure. That
is of course while the "html" identifier is still available to be evaluated.

The closure idea doesn't apply if the code argument to
setTimeout(code, delay) is a string. To paraphase Flanagan JavaScript
4th ed page 679:

 The statements in the string of setTimeout() execute in the context of window.

Nowadays the code argument can also be a function which is how
Prototype.js is using evalScripts(). Copying from this link

http://groups.google.com/group/comp.lang.javascript/tree/browse_frm/thread/6db7df20ac5c9a08/529bb25a7e5456aa?rnum=1&q=settimeout+function+argument&_done=%2Fgroup%2Fcomp.lang.javascript%2Fbrowse_frm%2Fthread%2F6db7df20ac5c9a08%2F312e04763fb3ee4c%3Flnk%3Dgst%26q%3Dsettimeout+function+argument%26rnum%3D1%26#doc_312e04763fb3ee4c

[start copy]

If the first argument to setTimeout (or setInterval) is a string,
the code is parsed and executed in the global scope and with "this"
referring to the global object.

If you change the first argument to a function expression, then that
function is called as a function, not as a method, so "this" again
refers to the global object, but the function closure retains its
scope chain.

This can be used to your advantage:

function timer() {
  alert(this.t);
  var self = this;
  setTimeout(function(){self.timer();}, this.t);
}

This causes the timeout to call a function that knows the current
"this" object (by the name "self"), and calls the "timer" method
on it as a method.

[end copy]
_______________________________________________
Rails-spinoffs mailing list
Rails-spinoffs@lists.rubyonrails.org
http://lists.rubyonrails.org/mailman/listinfo/rails-spinoffs

Reply via email to