The Linux Page

Javascript: setTimeout()

The other day, I wrote a module for Drupal. Up to here, no problem. What I wrote required a small amount of Javascript to make a dropdown menu feature work. That worked just fine under Mozilla and FireFox, and even Safari & Konqueror.

When I tried with Internet Explorer, nope. Not working at all.

The function I was using goes like this:

	setTimeout(<function name>, <# of milliseconds>[, <function parameters>]);

This is neat since you can call the named function and you in effect pass the <function parameters> to the function. This is very handy and looks like an object oriented programming scheme.

Internet Explorer does not support the last parameter. This means you need to use a global. So if you have to handle a time out for 10 different objects, you better have 10 different functions to handle each one of them. Luckily, you can do that in Javascript by creating a function in a variable. (Look at the AddThis.com code to see an example of that. They use it at length, so to speak!)