[转]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.setTimeoutdocument.addEventListener, or assigning to event handlers like click or submit.

时间: 2024-10-10 06:49:43

[转]setTimeout() 函数未定义错误的相关文章

Ubuntu gcc错误:对'log'等函数未定义的引用

a.c 1 #include <stdio.h> 2 #include <math.h> 3 int main() 4 { 5 float a; 6 void print_logarithm(double); 7 printf("enter a num:"); 8 scanf("%f",&a); 9 print_logarithm(a); 10 return 0; 11 } 12 void print_logarithm(double

IE8下提示&#39;console&#39;未定义错误

在开发的过程中由于调试的原因,在代码中加入console.info("xxxx"),而未进行删除 在IE8下测试该代码所在的页面报错,如下: 需要注意的是,使用console对象查看对象信息,在IE8浏览器下未打开开发人员工具(F12)的情况下 会报'console'未定义错误. 解决办法:1.打开开发人员调试工具(F12)                    2.注释掉该代码 IE8下提示'console'未定义错误

IE10,IE11下点击LinkButton出现_doPostBack未定义错误解决方法

出现的原因 .NET2.0和.NET4.0一起发布的浏览器定义文件中有一个错误,它们保存相当一部分浏览器版本的定义.但是浏览器的有些版本(比如IE10,11)则不再在这个范围之内.因此,ASP.NET把它们看做是未知的浏览器,默认降级处理,这样就会给用户带来不便,比如不支持JavaScript特性.而ASP.NET的服务器控件asp:LinkButton的渲染机制使得错误发生. 服务器控件: <asp:LinkButton ID="Logout" runat="serv

(转)JS之——解决IE6、7、8使用JSON.stringify报JSON未定义错误的问题

https://blog.csdn.net/l1028386804/article/details/53439755 在通过JavaScript将对象类型的参数通过JSON.stringify转换成字符串传递时,IE6.7.8会报:“JSON”未定义 的错误.我们可以通过在html文件的head头内引入json2.js文件来解决 <!--[if lt IE 9]> <scriptsrc="json2.js"></script><![endif]

js 自执行函数 函数内部可以通过函数名调用本身 函数外部调用报 未定义错误

先看错误过程 <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>ipt-test</title> </head> <body> <script> var a=true; !function main(){   if(a){     a=false;     main();   }   console.log('

Admob(6.12.x)符号未定义错误的解决方法(IOS)

在升级Admob的SDK版本到6.12.x时, 按照官方文档操作(https://developers.google.com/mobile-ads-sdk/docs/#ios), 添加如下framework: StoreKit AudioToolbox MessageUI SystemConfiguration CoreGraphics AdSupport 之后将-ObjC添加至应用目标构建设置中的Other Linker Flags. 编译,仍然会出现如下链接错误. Undefined sym

jQuery未定义错误原因(jQuery is not define)

使用jQuery时,必须把它写在最前面,这样浏览器才会先加载jQuery,否则会提示缺少对象. 正确 <script type="text/javascript" src="js/jquery/jquery-1.4.3.min.js"></script> <script type="text/javascript" src="js/custom.js"></script> 错误

codeblocks 多线程 pthread_create 函数未定义引用 解决办法

出现这种情况 要么函数名字写错 要么就是找不到定义函数的文件,在linux下面的多线程 pthread不是默认到库 需要自己链接下. 1. 可以在终端 编译: gcc pthread.c -lpthread -o pthread 2.可以在IDE 下面配置: Setting -> Compiler and debugger -> Linker Setting 根据自己的库的位置:ubuntu 12.04位置 可解决!

echarts 无法获取属性“getAttribute”的值: 对象为 null 或未定义 错误解决方法

使用百度的echarts时,在IE9下运行时有时会报如题的错误,有时刷新下又正常,造成这种错误的原因是 echarts.js引用放在head中或者放在body中HTML代码的前面了,造成加载时阻塞后面的html. 解决方法就是将echarts.js的引用放在</body>之前就可以了,完美解决!