Loop each forever

I want this loop forever

$(document).ready(function() {
  function news_hot() {
    $("div p").each(function(i) {
      $(this).delay(1000 * i).queue(function() {
        $(this).addClass('hot_li ');
        $(this).prev().removeClass('hot_li');
      });
    });
  }
  news_hot();

});
<div>
  <p>dfsdfsd</p>
  <p>dfsdfsd</p>
  <p>dfsdfsd</p>
</div>

Upvotes: 0

Views: 75

Answers (1)

Hemal
Hemal

Reputation: 3760

You can use setInterval() JavaScript function with specified time in milliseconds. The called function will run forever after interval time unless you stop it.

$(document).ready(function () {
        function news_hot() {
            $("div p").each(function (i) {
                $(this).delay(1000 * i).queue(function () {
                    $(this).addClass('hot_li ');
                    $(this).prev().removeClass('hot_li');
                });
            });
        }
        setInterval(news_hot(),5000);

    });

UPDATED CODE WORKING FIDDLE

function news_hot() {
        $("div p").each(function (i) {
            $(this).delay(1000 * i).queue(function () {
                $("div p").removeClass("hot_li");
                $(this).addClass('hot_li');
                //$(this).prev().removeClass('hot_li');
                $(this).dequeue();
            });
        });
}
setInterval(function(){news_hot()},5000);

.dequeue() function has been added to the code

Upvotes: 1

Related Questions