开发者

greasemonkey, jquery and setTimeout

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.jQu开发者_StackOverflow中文版ery == '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?


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!)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜