简体   繁体   English

我的setTimeout无法正常工作

[英]my setTimeout doesn't work correctly

this is the code: 这是代码:

var stripeAnimation = function() {

var streetDivWidth = $('.street_name').width();
var streetFull = $('.street_name .street_name_text');

for(var i=0; i<streetFull.length; i++) {
    var item = $(streetFull[i]);
    var widthFull = item.width();
    var remainder = widthFull - streetDivWidth;
    var animationSpeed = widthFull * 5;
    var summary = streetDivWidth - widthFull;
    if(summary < 0) {
        item.children('.gradient_span').addClass('gradient');
        infinite();
        setTimeout(infinite, 1000);
    }

}
function infinite() {
    item.animate({
        marginLeft: '-' + remainder + 'px'
    }, animationSpeed).animate({
            marginLeft: 0
        }, widthFull).delay(1000);
}

}
$(document).ready(function() {
    stripeAnimation();
});

It looks like it should loop the animation over and over in a delay of 1000ms - "setTimeout(infinite, 1000);". 看起来应该以1000毫秒的延迟一遍又一遍地循环播放动画-“ setTimeout(infinite,1000);”。 but it doesn't. 但事实并非如此。 Please help! 请帮忙!

If you want to loop, then you need to use setInterval() 如果要循环,则需要使用setInterval()

setInterval(infinite, 10000);

then 然后

function infinite() {
    item.animate({
        marginLeft: '-' + remainder + 'px'
    }, animationSpeed).animate({
            marginLeft: 0
        }, widthFull);
}

Remainder variable is local and u are using it as like global function. 其余变量是局部变量,您像全局函数一样使用它。 should declare it globally. 应该在全局进行声明。

setTimeout will call your function only once but, as Arun P said, you have the choice of using setInterval , only it isn't recommended for animation. setTimeout将仅调用一次函数,但是,正如Arun P所说,您可以选择使用setInterval ,但不建议将其用于动画。 The problem with setInterval is that the delay interval you specify is the minimum time until it will be called but not a promise it will be called when that interval is over. setInterval的问题在于,您指定的延迟间隔是直到它被调用之前的最短时间,但是不能保证在该间隔结束后将被调用。

An example will be, if you set an interval of 300 milliseconds but the queue was held up by other operations (UI or other JS operations) for, let's say, 600 milliseconds your function will be called twice, one after the other with no delay, which will make your animation uneven. 一个示例是,如果您设置间隔为300毫秒,但队列被其他操作(UI或其他JS操作)阻塞了,例如600毫秒,则函数将被调用两次,一个接一个地进行,没有延迟,这会使您的动画不均匀。 You can't ensure a timeout will be called at exactly your interval, only no less than that interval. 您不能确保会在您的间隔恰好调用超时,但不能小于该间隔。

A better approach would be to make your first initial call with a setTimeout, as you have done, and again at the end of the infinite() function call setTimeout(infinite, 1000) again. 更好的方法是像您一样,使用setTimeout进行首次首次调用,然后在infinite()函数结束时再次调用setTimeout(infinite, 1000)

With all that said, if it's applicable, the best way to do animations is the requestAnimationFrame method, you can look into it here: 综上所述,如果适用,制作动画的最佳方法是requestAnimationFrame方法,您可以在此处进行研究:

https://developer.mozilla.org/en-US/docs/Web/API/window.requestAnimationFrame https://developer.mozilla.org/zh-CN/docs/Web/API/window.requestAnimationFrame

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

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