setInterval 在 Google Chrome 扩展程序中不起作用(仅触发一次) [英] setInterval not working (firing only once) in Google Chrome extension
本文介绍了setInterval 在 Google Chrome 扩展程序中不起作用(仅触发一次)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
正如标题所说:setInterval
只触发一次回调.
Just as the title says: setInterval
is only firing its callback once.
manifest.json:
manifest.json:
{
//...
"content_scripts" : [{
"js" : ["code.js"],
//...
}],
//...
}
code.js(示例):
code.js (example):
setInterval(alert('only shown once'),2000);
为什么,我该如何解决?该代码在扩展程序之外运行良好(即使在书签中).
Why, and how I could fix it? The code works well outside of an extension (even in a bookmarklet).
推荐答案
setInterval(function() { alert('only shown once') },2000);
您需要传递像 alert
这样的函数引用,而不是返回值 alert()
You need to pass a function reference like alert
and not a return value alert()
这篇关于setInterval 在 Google Chrome 扩展程序中不起作用(仅触发一次)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文