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 ![]()
Monday 26.10.2009 on 03:06
very useful post, thank you very much for sharing.
Monday 26.10.2009 on 11:26
Good point
Monday 26.10.2009 on 19:36
tnx
ax
Tuesday 27.10.2009 on 07:34
We add this code where my friend!.
Tuesday 27.10.2009 on 08:31
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
Wednesday 25.11.2009 on 21:33
Nice Tut Thanx
Wednesday 25.11.2009 on 21:41
np
Ax