views:

286

answers:

1

Hi, I'm playing around with jquery and greasemonkey and tried to use setTimeout to call a function every second, but for some reason it doesn't work.

// Add jQuery
var GM_JQ = document.createElement('script');
GM_JQ.src = 'http://jquery.com/src/jquery-latest.js';
GM_JQ.type = 'text/javascript';
document.getElementsByTagName('head')[0].appendChild(GM_JQ);

// Check if jQuery's loaded
function GM_wait() {
    if(typeof unsafeWindow.jQuery == 'undefined') { window.setTimeout(GM_wait,100); }
else { $ = unsafeWindow.jQuery; letsJQuery(); }
}
GM_wait();

// All your GM code must be inside this function
function letsJQuery() {

function timer() { alert('TEST') }

setTimeout(timer, 1000);

}

any ideas?

A: 

There are issues with the way you're assessing if jQuery is loaded correctly, I suggest you Google for a better way of embedding jQuery via GreaseMonkey (I do have some scripts somewhere doing this but not on this machine).

However to do what you're trying you don't actually need any jQuery; simply:

function timer() {
  console.log('TEST')
  setTimeout(timer, 1000);
}

timer();

Works just fine :)

(Note I changed alert to console.log, which sends output to Firebug console; alert would cause pain!)

Steve