简体   繁体   English

JavaScript setInterval将仅运行一次jQuery

[英]JavaScript setInterval will only run jQuery once

I have a fiddle here: http://jsfiddle.net/justinboyd101/4nrbb/ 我在这里有一个小提琴: http : //jsfiddle.net/justinboyd101/4nrbb/

Here is my question: 这是我的问题:

Why are my console.log function and my statement to add html to a div working, while it's not running on my other looping jQuery function? 为什么当我的console.log函数和将html添加到div的语句没有在我的其他循环jQuery函数上运行时,为什么起作用?

For more details, please read below: 有关更多详细信息,请阅读以下内容:

I'm trying to create a news ticker using setInterval. 我正在尝试使用setInterval创建一个新闻动态。 The animation is supposed to slide text in from the right side of the screen, then push the content down, then reset back to its original position. 该动画应该从屏幕的右侧滑动文本,然后向下推内容,然后重设回其原始位置。 For some reason it will only run animations once. 由于某种原因,它将只运行一次动画。 I've set up a simple function where ever interval will gather information from the array and add it to an empty div. 我设置了一个简单的函数,在该函数中,interval都会从数组中收集信息并将其添加到空div中。

Here is my code if you do not wish to go to jsfiddle: 如果您不想去jsfiddle,这是我的代码:

HTML 的HTML

<div id="newsValueText" class="newsValueText floatLeft darkBlue">
    <ul id="newsTicker" class="newsTicker">
        <li name="tick" ph="tick1">We have in-store technical support.</li>
        <li name="tick" ph="tick2">Option 2</li>
        <li name="tick" ph="tick3">Option 3</li>
        <li name="tick" ph="tick4">Option 4</li>
        <li name="tick" ph="tick5">Option 5</li>
        <li name="tick" ph="tick6">Option 6</li>
    </ul>
</div>
<div id="log"></div>

JS JS

//Get the ticker elements
var tickText = document.getElementById('newsTicker').getElementsByTagName('li');
var tickNum = tickText.length;
//New Array to clear empty data
var tickArry = [];
//Loop through list items and add non-void data to new loop
for(var a = 0; a < tickNum; a++) {
    if ($(tickText[a]).html() != '') {
        tickArry.push($(tickText[a]));
    }
}
//Loop int
var i = 0;
var log = document.getElementById('log');
//Self-invoking function for ticker animation
function animTick() {
    console.log($(tickArry[i]));
    log.innerHTML += $(tickArry[i]).html();
    //Make current tick item fall down
        $(tickArry[i]).animate({'padding-top' : '+=50px'}, 500,
            function() {
                //Once tick item is off screen, reset values
                $(this).css({'margin-left' : '9999px', 'padding-top' : '0'});
            });
        //Increment item
        i++;
        //If the tick has reach its length then reset
        if (i === tickNum) {
            i = 0;
        //Else call the animation for the next tick
        }
    }
    //Immediately animate in first list item
    $(tickArry[i]).animate({'margin-left' : '0px'}, 2000);
    //Add a delay
    setInterval(animTick, 3000);

Use setTimeout() if you are recursively calling the same function. 如果要递归调用同一函数,请使用setTimeout()

Your setInterval() alternative would be: 您的setInterval()替代方法是:

function animTick() {
    // Function definition
}

setInterval(animTick, 3000);

What if you include $(tickArry[i]).animate({'margin-left' : '0px'}, 2000); 如果包含$(tickArry[i]).animate({'margin-left' : '0px'}, 2000);怎么办? inside your animTick function? 里面你animTick功能?

Demo: Fiddle 演示: 小提琴

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

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