【问题标题】:Can anyone explain why this JavaScript wont run谁能解释为什么这个 JavaScript 不能运行
【发布时间】:2013-04-16 21:04:24
【问题描述】:

我对 JS 不太了解,我只是不明白为什么这不起作用! 该代码使用 jquery 将脉动效果应用于我的一个 div 并永远运行,除非我用另一个函数停止它,但我无法弄清楚为什么我的第一段代码不会运行!

function animate(var x){
    // Do pulsate animation
    $(x).effect("pulsate", { times:4 }, 5000);
    // set timeout and recall after 10secs
    setTimeout(animate, 10000);
}
  $(document).ready(animate("#mydiv"));

让它工作的唯一方法是我这样做

function animate(){
    // Do pulsate animation
    $("#mydiv").effect("pulsate", { times:4 }, 5000);
    // set timeout and recall after 10secs
    setTimeout(animate, 10000);
}
  $(document).ready(animate);

请注意,在第一个 sn-p 中,代码使用变量更有用,而第二个部分将选择器名称硬编码

【问题讨论】:

    标签: javascript jquery jquery-ui web-applications


    【解决方案1】:

    不要在你的函数声明中使用var。只需使用:

    function animate(x){
    

    另外,您的第一个示例可能需要这样的东西:

    function animate(x){
        return function () {
            function animateInner() {
                $(x).effect("pulsate", { times:4 }, 5000);
                setTimeout(animateInner, 10000);
            }
            animateInner();
        };
    }
    $(document).ready(animate("#mydiv"));
    

    演示: http://jsfiddle.net/XHKbC/

    否则,原始的animate("#mydiv") 调用会立即执行(并且$(x) 可能找不到任何东西,因为DOM 还没有准备好)。 $(document).ready() 需要对函数的引用。你调用了一个函数。但这有点矫枉过正。只需使用:

    $(document).ready(function () {
        animate("#mydiv");
    });
    

    但您必须更改您的函数,以便 setTimeout 也传递 x 的值:

    function animate(x){
        // Do pulsate animation
        $(x).effect("pulsate", { times:4 }, 5000);
        // set timeout and recall after 10secs
        setTimeout(function () {
            animate(x);
        }, 10000);
    }
    

    演示: http://jsfiddle.net/XHKbC/2/

    虽然它的代码/复杂一点,但我的第一个示例在我的第二个示例中并没有遇到问题(必须在 setTimeout 中传递 x)通过使用闭包。

    更新:

    看到你是如何使用这段代码的,我会这样设置:

    function Animater(target) {
        var self = this;
        var animateTO;
        var animateElement = target;
    
        function animate() {
            animateElement.effect("pulsate", { times:4 }, 5000);
            animateTO = setTimeout(animate, 10000);
        }
    
        self.start = function () {
            animate();
        };
    
        self.stop = function () {
            animateElement.finish();
            clearTimeout(animateTO);
        };
    }
    

    并创建一个新的,例如:

    var mydivAnimater = new Animater($("#mydiv"));
    

    然后您可以在其上调用.start().stop(),并根据需要在不同元素上创建任意数量的Animater 对象。

    演示: http://jsfiddle.net/K7bQC/3/

    【讨论】:

    • 嘿嘿,你想狠狠地揍我,是吗? ^_^
    • @Christoph 哈哈,我并不是要反对你。我注意到的第一件事是var,所以我发布了。后来才意识到函数一开始就乱七八糟,所以我尝试重构,做了太多,所以我只想包含我想到的选项:)
    • +1 好答案。解释和演示对于寻找类似解决方案的未来用户总是有用的。
    • @FrançoisWahl 非常感谢,这就是我想做的:)
    • @Ian 你可以展示如何停止动画吗?我已经厌倦了这个jsfiddle.net/W7eES(基于你的小提琴)但它不会停止:S
    【解决方案2】:

    您的代码有两个问题:

    省略变量:

    function animate(x){
    

    修改您的事件处理程序:

    $(document).ready(function(){
       animate("#mydiv");
    });
    

    您需要交出一个函数引用(animatefunction(){}),如果您传递animate(),则不要立即运行您正在执行的代码。

    现在,为了不丢失对您的 x 的引用,您还必须在超时时修改动画调用:

    setTimeout(function () {
            animate(x);
        }, 10000);
    

    【讨论】:

    • Oooooooh 所以我需要总是传递一个引用。太酷了!
    • @Termiux 当然,这是一个回调。如果您不传递广告引用,您将立即执行回调并将其返回值传递给 DOMready。
    • @Christoph:$(document).ready(animate.bind(this, "#mydiv")); 会起作用吗?
    • @FrançoisWahl 不,它在animate 函数中设置this 的值。所以从技术上讲,如果函数开始使用this 而不是x,它就可以工作。
    • @Ian:抱歉,我注意到了这一点并更新了评论以传递 this, argument。这行得通吗?
    【解决方案3】:

    指定函数参数时不需要输入var

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-04-10
      • 1970-01-01
      • 2021-05-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多