Greasemonkey、jquery 和 setTimeout
我正在使用 jquery 和greasemonkey,并尝试使用 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() {
函数定时器() { 警报('测试') setTimeout
(计时器, 1000);
}
有什么想法吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您评估 jQuery 是否正确加载的方式存在问题,我建议您通过 Google 搜索通过 GreaseMonkey 嵌入 jQuery 的更好方法(我确实有一些脚本在某处执行此操作,但不在本机上)。
然而,要做你正在尝试的事情,你实际上并不需要任何 jQuery;简单地说:
工作得很好:)
(注意我将警报更改为console.log,它将输出发送到Firebug控制台;警报会引起痛苦!)
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:
Works just fine :)
(Note I changed alert to console.log, which sends output to Firebug console; alert would cause pain!)