[转]setTimeout() 函数未定义错误
用 setTimeout("showMe()",1000) 时出现 showMe is not defined 错误。这是由于showMe() 函数不在 setTimeout 调用环境中。转载的这篇文章解释并解决了这一问题。原标题为: 2.3. Coding your user script ,节选自 Dive Into Greasemonkey
可在这里免费下载此书 http://diveintogreasemonkey.org/
2.3. Coding your user script
Our first user script simply displays an alert saying “Hello world!” when it is executed.
Example: Display the “Hello world!” alert
alert('Hello world!');
Although this code looks obvious enough, and does exactly what you would expect, Greasemonkey is actually doing a number of things behind the scenes to ensure that user scripts do not interact badly with other scripts defined by the original page. Specifically, it automatically wraps your user script in an anonymous function wrapper. Ordinarily you can ignore this, but it will eventually creep up and bite you in the ass, so you may as well learn about it now.
One of the most common ways this can bite you is that variables and functions that you define in a user script are not available to other scripts. In fact, they are not available at all once the user script has finished running. This means that you will run into problems if you are expecting to be able to call your own functions later by using thewindow.setTimeout function, or by setting string-based onclick attributes on links and expecting Javascript to evaluate your function names later.
For example, this user script defines a function helloworld, then attempts to set a timer to call it one second later.
Example: Bad way to delay calling a function
function helloworld() {
alert('Hello world!');
}
window.setTimeout("helloworld()", 60);
This will not work; no alert will be displayed. If you open JavaScript Console, you will see an exception displayed: Error: helloworld is not defined. This is because, by the time the timeout expires and the call to helloworld() is evaluated, the helloworld function no longer exists.
If you need to reference your user script's variables or functions later, you will need to explicitly define them as properties of the window object, which is always available.
Example: Better way to delay calling a function
window.helloworld = function() {
alert('Hello world!');
}
window.setTimeout("helloworld()", 60);
This works as expected: one second after the page loads, an alert pops up proudly displaying “Hello world!”
However, setting properties on window is still not ideal; it's a bit like using a global variable when a local one will do. (Actually, it's exactly like that, since window is global and available to all scripts on the page.) More practically, you could end up interfering with other scripts that were defined on the page, or even other user scripts.
The best solution is to define an anonymous function yourself and pass it as the first argument to window.setTimeout.
Example: Best way to delay calling a function
window.setTimeout(function() { alert('Hello world!') }, 60);
What I'm doing here is creating a function without a name (an “anonymous function”), then immediately passing the function itself to window.setTimeout. This accomplishes the same thing as the previous example, but it leaves no trace, i.e. it's undetectable to other scripts.
I find that I use anonymous functions regularly while writing user scripts. They are ideal for creating “one-off” functions and passing them as arguments to things likewindow.setTimeout, document.addEventListener, or assigning to event handlers like click or submit.
[转]setTimeout() 函数未定义错误的更多相关文章
- Ubuntu gcc错误:对'log'等函数未定义的引用
Ubuntu gcc错误:对'log'等函数未定义的引用 a.c #include <stdio.h>#include <math.h>int main(){ float ...
- __flash__removeCallback 未定义错误
使用swfupload作为上传组件,artdialog作为弹出窗口,在关闭弹出窗口时,出现"__flash__removeCallback"未定义错误.而且是关了又出现.网上有些解 ...
- IE10,11下_doPostBack未定义错误的解决方法
出现的原因 .NET2.0和.NET4.0一起发布的浏览器定义文件中有一个错误,它们保存相当一部分浏览器版本的定义.但是浏览器的有些版本(比如IE10,11)则不再在这个范围之内.因此,ASP.NET ...
- IIS7下ajax报未定义错误
项目之前在iis6环境下运行的很好,今天在WIN7下发布,结果居然报对象未定义错误,经过个把小时折腾,终于弄清楚原委. 在web.config中关于AjaxPro的设置,在IIS7.0(WIN7中使用 ...
- ASP.Net IE10 _doPostBack 未定义错误【转】
--昨天发现IE10下面ReportViewer执行报表会报错,发现为Js报_doPostBack 未定义错误,查找相关资料发现问题为 当前framework不能识别IE10版本,把该浏览器做降级处理 ...
- IE8下提示'console'未定义错误
在开发的过程中由于调试的原因,在代码中加入console.info("xxxx"),而未进行删除 在IE8下测试该代码所在的页面报错,如下: 需要注意的是,使用console对象查 ...
- webpack+babel项目在IE下报Promise未定义错误引出的思考
低版本浏览器引起的问题 最近开发一个基于webpack+babel+react的项目,一般本地是在chrome浏览上面开发,chrome浏览器开发因为支持大部分新的js特性,所以一般不怎么需要poly ...
- lager_transform未定义错误
lager_transform未定义错误rebar编译时报错:D:\server\six>d:/tools/rebar/rebar.cmd compile==> mysql (compil ...
- (转)JS之——解决IE6、7、8使用JSON.stringify报JSON未定义错误的问题
https://blog.csdn.net/l1028386804/article/details/53439755 在通过JavaScript将对象类型的参数通过JSON.stringify转换成字 ...
随机推荐
- 前端模块化开发篇之grunt&webpack篇
几个月前写了一篇有关gulp和browserify来做前端构建的博客,因为browserify用来做js的打包时可能有些麻烦(特别是在写React的时候),所以这里再强烈推荐一款js打包工具-webp ...
- Bzoj 1726: [Usaco2006 Nov]Roadblocks第二短路 dijkstra,堆,A*,次短路
1726: [Usaco2006 Nov]Roadblocks第二短路 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 969 Solved: 468[S ...
- Project Euler 9
题意:三个正整数a + b + c = 1000,a*a + b*b = c*c.求a*b*c. 解法:可以暴力枚举,但是也有数学方法. 首先,a,b,c中肯定有至少一个为偶数,否则和不可能为以上两个 ...
- 如何设置textarea光标默认为第一行第一个字符
判断文本区是否有内容,如果没有那么光标肯定是在第一行第一个为止的,记住,空格回车也算是有内容在的,也会影响光标的位置
- nyoj 116 士兵杀敌(二)【线段树单点更新+求和】
士兵杀敌(二) 时间限制:1000 ms | 内存限制:65535 KB 难度:5 描述 南将军手下有N个士兵,分别编号1到N,这些士兵的杀敌数都是已知的. 小工是南将军手下的军师,南将军经常 ...
- Nice validator领先的表单验证解决方案 转
Nice validator是一个简单智能的Web表单验证插件,可以验证现有的所有格式,比如邮箱地址.电话号码等,您还可以自定义规则验证,插件基于jQuery库,支持多种语言配置. 安装 1.您可以访 ...
- SQL GROUP BY GROUPING SETS,ROLLUP,CUBE(需求举例)
实现按照不同级别分组统计 关于GROUP BY 中的GROUPING SETS,ROLLUP,CUBE 从需求的角度理解会更加容易些. 需求举例: 假如一所学校只有两个系, 每个系有两个专业, 每个专 ...
- 统计学习导论:基于R应用——第三章习题
第三章习题 部分证明题未给出答案 1. 表3.4中,零假设是指三种形式的广告对TV的销量没什么影响.而电视广告和收音机广告的P值小说明,原假设是错的,也就是电视广告和收音机广告均对TV的销量有影响:报 ...
- java 递归函数
一.递归函数,通俗的说就是函数本身自己调用自己... 如:n!=n(n-1)! 你定义函数f(n)=nf(n-1) 而f(n-1)又是这个定义的函数..这就是递归 二.为什么要用递归:递归的目 ...
- fscanf函数
函数定义: int fscanf( FILE *stream, const char *format [, argument ]... ); 以下是csdn的样例: /* FSCANF.C: This ...