简体   繁体   English

Javascript函数仅在Firefox上运行一次

[英]Javascript function is working on firefox only once

I have the following javascript function: 我具有以下javascript函数:

function kitemoving() {
    setInterval(function(){
        kite.animate({
            top: '+=30'
        },{ 
            duration: 1500 
        });
        kite.animate({
            top: '-=30'
        },{ 
            duration: 1500
        });
    });
}
kitemoving();

Here is the Fiddle 这是小提琴

Everywhere it is working fine only on Mozilla Firefox all the movements with this code are being worked only once. 仅在Mozilla Firefox上运行正常的所有地方,使用此代码的所有动作都只能运行一次。 Could you help me to understand why? 你能帮我理解为什么吗?

Don't use interval, you could easily saturate the fx queue. 不要使用间隔,您可以轻松地使fx队列饱和。 Instead, use any complete animation callback, eg: 相反,请使用任何完整的动画回调,例如:

var kite = jQuery('.kite');

function kitemoving() {
    kite.animate({
        top: '+=30'
    }, {
        duration: 1500
    });
    kite.animate({
        top: '-=30'
    }, {
        duration: 1500
    }).promise().done(kitemoving);
}
kitemoving();

-jsFiddle- -jsFiddle-

You have to specify a value for the delay parameter. 您必须为delay参数指定一个值。

setInterval expects at least two parameters. setInterval至少需要两个参数。 Quoting from MDN MDN报价

func is the function you want to be called repeatedly. func是要重复调用的函数。

delay is the number of milliseconds (thousandths of a second) that the setInterval() function should wait before each call to func. delay是setInterval()函数在每次调用func之前应等待的毫秒数(千分之一秒)。 As with setTimeout, there is a minimum delay enforced. 与setTimeout一样,将强制执行最小延迟。

param1, param2, and so forth, are additional parameters that are passed through to the function specified by func. param1, param2,等是传递给func指定的函数的其他参数。

Therefore when you omit the second parameter you can not expect a defined behaviour. 因此,当您省略第二个参数时,您将无法期望已定义的行为。

All you have to do in order to make your fiddle work in Firefox, is provide an interval amount to the setInterval call 为了使您的小提琴在Firefox中工作,您要做的就是为setInterval调用提供间隔时间

setInterval(function(){
    ....
}, 3000);

Here is an updated fiddle, which works in Firefox: http://jsfiddle.net/wdodmvyu/5/ 这是一个更新的小提琴,可在Firefox中使用: http : //jsfiddle.net/wdodmvyu/5/

You did not specify an interval value that is why Gecko is struggling. 您没有指定间隔值,这就是壁虎挣扎的原因。

function kitemoving() {
    setInterval(function(){
        kite.animate({
            top: '+=30'
        },{ 
            duration: 1500 
        });
        kite.animate({
            top: '-=30'
        },{ 
            duration: 1500
        });
    },300);//interval
}
kitemoving();

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

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