Function is defined, but Error says.. Function is not found ! (Strange)
If you are using GreaseMonkey, any functions you define are sandboxed by GM and not available in the main window.
When you use any of the native functions however, like setTimeout or alert, they are called in the context of the main window e.g;
when you call setTimeout you are actually calling window.setTimeout()
Now the function you have defined, mark doesn't exist in the main window and what you are asking setTimeout to do is evaluate the string 'mark()'. When the timeout fires
window.eval( 'mark()' )
is called and as discussed, window.mark is not defined. So you have a couple of options:
1) Define mark on the window object. GM allows you to do this through the unsafeWindow object like this:
unsafeWindow.mark = function(){}
setTimeout( 'mark()', 10 ); //this works but is ugly, it uses eval
2) Pass a reference to the local mark to setTimeout:
function mark(){}
setTimeout( mark, 10 ); //this works too but you can't send parameters
But what if you need to send parameters? If you have defined your function on the main window, the eval method will work (but it is ugly - don't do it)
unsafeWindow.mark2 = function( param ) {
alert( param )
}
setTimeout( 'mark2( "hello" )', 10 ); //this alerts hello
But this method will work for functions with parameters whether you have defined them on the main window or just in GM The call is wrapped in an anonymous function and passed into setTimeout
setTimeout( function() {
mark2( "hello" )
}, 10 ); //this alerts hello
try to use thissetTimeout(mark,5000);