Showing posts with label setTimeout. Show all posts
Showing posts with label setTimeout. Show all posts

Monday, July 18, 2011

How can I pass a parameter to a setTimeout() callback?

I am getting "t is not defined" error when trying to execute the following code:

TaskList.prototype.GetTaskItems = function() {
var t = this;
%$.ajax({
...,
success: function(data) {...},
error: function() {
setTimeout("t.GetTaskItems()", 30000);
}
});
}

Not too sure why it doesn't work, but here's a workaround:
setTimeout(function() {
t.GetTaskItems();
, 30000);

Sunday, December 13, 2009

Javascript: setTimeout or setInterval

The setTimeout function delays for a specified time period and then triggers execution of a specified function. Once the function is triggered the setTimeout has finished. Use the clearTimeout function to terminate the execution of the setTimeout.
Example of use: Set a timelag before drop-down menu closes after a mouseout event.
Code: setTimeout("closeMenu()", delay)

The setInterval function also delays for a specified time before triggering the execution of a specific function. However, the command doesn't complete after triggering that function. Instead it waits for a specified time again and then triggers the function again. This process continues (at specified intervals) until page is unloaded or the clearInterval function is called.
Example of use: Slide-in and slide-out animations for the drop-down menu. At every interval, set length and opacity accordingly.
Code: setInterval("ddMenu()", interval)