Axino.net

javascript setTimeout() in greasemonkey

by Arxleol on Sunday 25.10.2009, under javascript, tutorial

Yesterday, I was writing some function that should be delayed so of course I used setTimeout() function but things are not as simple as when script is executed in sandbox mode of Greasemonkey!Let’s say that I want to change some value each second. And therefore I wrote function that will do this(example function):

window.changeX = function(){
 
x=x-1;
 
}

Now I wanted this value changed each second. I called function setTimeout as you can see:

window.setTimeout("changeX()",1000);

Code in between exclamation marks supposed to be executed after second. Second number represents one second as parameter offered has to be in milliseconds.

However, problem with Greasemonkey is that the function changeX will not exit after one second, as execution of the script has finished. So solution to this problem is to call function by reference. As following:

window.setTimeout(changeX, 1000);

Seems like simple solution but to figure it out I needed time and nerves, you don’t need to spend :D

Similar Posts:

:, , ,
7 comments for this entry:
  1. JavaScript Countdown Timer

    very useful post, thank you very much for sharing.

  2. webdragon

    We add this code where my friend!.

  3. Arxleol

    I guess you may use it wherever you want. But it is not connected directly to the KingsAgeX project. Though it is used in version that will be released during following week.

    Ax

  4. Keith

    Nice Tut Thanx

Leave a Reply

Looking for something?

Use the form below to search the site:

Still not finding what you're looking for? Drop a comment on a post or contact us so we can take care of it!

Archives