[Go]settimeout () function undefined error

Source: Internet
Author: User

showMe is not defined error with SetTimeout ("ShowMe ()", 1000). This is because the SHOWME () function is not in the setTimeout call environment. Reproduced in this article explains and solves this problem. The original title is: 2.3. Coding your user script, excerpt from Dive into Greasemonkey

You can download this book for free here http://diveintogreasemonkey.org/

2.3. Coding your user script

Our first user script simply displays a alert saying "Hello world!" when it was executed.

Example:display the "Hello world! "alert
Alert ('Hello world!');

Although this code looks obvious enough, and does exactly what's would expect, Greasemonkey is actually doing a numbe R of things behind the scenes to ensure that user scripts does not interact badly with other scripts defined by the original Page. Specifically, it automatically wraps your user script in an anonymous function wrapper. Ordinarily can ignore this, but it'll eventually creep up and bite your in the the-the- Now.

One of the most common ways this can bite it variables and functions so you define in a user script was not Available to other scripts. In fact, they is not available at all once the user script has finished running. This means, you'll run into problems if is expecting to being able to call your own functions later by using the function, or by setting string-based onclick attributes in links and expecting Javascript to evaluate your function name s later.

For example, the This user script defines a function helloworld , then attempts to set a timer to call it one second later.

Example:bad to delay calling a function
function  HelloWorld() {    alert('Hello world!' );} window. SetTimeout ("HelloWorld()", 60);

This won't work; No alert would be displayed. If you open a JavaScript Console, you'll see a exception displayed: This Error: helloworld is not defined. was because, by the time the timeout Expir Es helloworld() and the call are evaluated, the helloworld function no longer exists.

If you need to reference your user script ' s variables or functions later, you'll need to explicitly define them as Prope Rties window of the object, which is always available.

Example:better 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, a alert pops up proudly displaying "Hello world!"

However, setting properties on was window still not ideal; it's a bit like using a global variable when a local one would do. (Actually, it's exactly like that, since are global and available to all scripts on the window page.) More practically, could end up interfering with other scripts that were defined on the page, or even other user script S.

The best solution are to define a anonymous function yourself and pass it as the first argument to window.setTimeout .

Example:best to delay calling a function
window. SetTimeout (functionalert('Hello world!')}, 60);

What am I ' m doing here's creating a function without a name (an "anonymous function") and then immediately passing the Func tion 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 is ideal for creating "one-off" functions and passing them as arguments to things like window.setTimeout , document.addEventListener , or assigning To event handlers like click or submit .

[Go]settimeout () function undefined error

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.