简体   繁体   English

谁能解释为什么这个JavaScript无法运行

[英]Can anyone explain why this JavaScript wont run

Im not very good wit JS and I just dont get why this wont work! 我不是很好的机智JS,我只是不明白为什么这行不通! The code uses jquery to apply the pulsate efect to one of my divs and run forever unless I stop it with another function, but I cannot figure our why my first piece of code wont run! 该代码使用jquery将pulsate效果应用于我的一个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"));

Only way to get it working is for me to do this 使它工作的唯一方法是我这样做

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

Note that in the first snippet the code uses variables to be more useful and the second piece has the selectors name hardcoded 请注意,在第一个代码段中,代码使用变量更加有用,第二个代码中的选择器名称已硬编码

Don't use var in your function declaration. 不要在函数声明中使用var Just use: 只需使用:

function animate(x){

Also, you probably want something like this for your first example: 另外,您的第一个示例可能想要这样的东西:

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

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

Otherwise, the original animate("#mydiv") call executes immediately (and $(x) probably won't find anything since the DOM isn't ready yet). 否则,原始的animate("#mydiv")调用将立即执行(由于DOM尚未准备就绪, $(x)可能找不到任何内容)。 $(document).ready() expects a reference to a function. $(document).ready()需要对函数的引用。 You called a function instead. 您改为调用一个函数。 But that's all a little overkill. 但是,这全都有些过分了。 Just use: 只需使用:

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

but you'll have to change your function so the setTimeout passes the value of x as well: 但是您必须更改函数,以便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);
}

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

Although it's a little more code/complex, my first example doesn't suffer the problem in my second (having to pass x in the setTimeout ) by using a closure. 尽管代码/复杂程度更高,但我的第一个示例在第二个示例中并没有遇到使用闭包的问题(必须在setTimeout传递x )。

UPDATE: 更新:

Being shown how you are using this code, I'd set it up like this: 在显示您如何使用此代码的过程中,我将其设置如下:

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);
    };
}

And create a new one like: 并创建一个新的像:

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

You can then call .start() and .stop() on it, and you create any number of these Animater objects on different elements as you want. 然后可以对其调用.stop() .start().stop() ,然后根据需要在不同元素上创建任意数量的这些Animater对象。

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

Your code has two issues: 您的代码有两个问题:

omit the var: 忽略变量:

function animate(x){

modify your event handler: 修改您的事件处理程序:

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

You need to hand over a function reference (either animate or function(){} ), not run the code right away which you are doing if you pass animate() . 您需要交出函数引用( animatefunction(){} ),而不是立即通过animate()运行正在执行的代码。

Now to not lose the reference to your x you have to modify the animate call in the timeout too: 现在,为了不丢失对x的引用,您还必须修改超时中的动画调用:

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

指定功能参数时无需键入var

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM