2010-02-19 34 views
1

我玩弄jquery和greasemonkey,並試圖使用setTimeout每秒調用一個函數,但由於某種原因它不起作用。greasemonkey,jquery和setTimeout

// 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() { 

功能定時器(){ 警報( 'TEST') }

的setTimeout(定時器,1000);

} 

有什麼想法嗎?

+0

最新版本的jQuery犯規的在Greasemonkey工作。在GM中工作的更高版本是1.3.2,並且我建議你添加一個eventlistener(on load)到GM_JQ,所以你可以避免使用GM_wait() – w35l3y 2010-08-08 19:10:09

回答

0

如果您正在評估jQuery的正確加載方式有問題,我建議您通過GreaseMonkey嵌入jQuery(我確實有一些腳本在某處做這個但不是在這臺機器上)的Google。

但是,要做你想要的東西,你實際上並不需要任何jQuery;簡單地說:

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

timer(); 

工作得很好:)

(請注意,我改變警覺CONSOLE.LOG,它發送輸出到Firebug的控制檯,警報會引起疼痛!)

+0

請使用'''setInterval()'''for以上功能:) – user1931751 2016-11-16 12:16:37

+0

你說得對,'setInterval()'可能會更好,拿7年前我 – Steve 2016-11-19 10:15:43