zoukankan      html  css  js  c++  java
  • [转]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.

  • 相关阅读:
    批处理向FTP上传指定属性的文件 批处理增量备份的例子
    基于PHPExcel常用方法总结(phpexcel类库实例)
    Expo大作战(三十四)--expo sdk api之LinearGradient(线性渐变),KeepAwake(保持屏幕不休眠),IntentLauncherAndroid,Gyroscope,
    Expo大作战(三十三)--expo sdk api之MapView(地图),MailComposer(磁力传感计),Lottie(动画)
    Expo大作战(三十二)--expo sdk api之Noifications
    Expo大作战(三十一)--expo sdk api之Payments(expo中的支付),翻译这篇文章傻逼了,完全不符合国内用户,我只负责翻译大家可以略过!
    Expo大作战(三十)--expo sdk api之Permissions(权限管理模块),Pedometer(计步器api)
    生成分类编号的方法
    牛腩学Kotlin做Android应用
    我用Xamarin开发android应用,应用在真机上一打开就退出了
  • 原文地址:https://www.cnblogs.com/go-jzg/p/4528614.html
Copyright © 2011-2022 走看看