0

I'm using setInterval with jQuery delay.

But the delay() inside setInterval seems not to be working or it does not wait for 3 seconds (in setInterval).

My goal:

  • wait 3 seconds first
  • print Hello word 10
  • then wait 2 seconds to fadeout
  • wait 3 seconds
  • print hello word 9
  • and so on...

The snippet below shows it only waits 2 seconds and prints..

count = 10;
// store setInterval ID in var
var interval = setInterval(function(){
  // log value of count
  console.log(count);

   $('.output').append(
    " hello world"+count+"<br>"
    ).hide().fadeIn(1000).delay(2000).fadeOut('slow');

    if(count <= 0) {
      // if count reaches 10 the clear using interval id
      clearInterval(interval);
    } else {
      // otherwise increment count
         count--;
     }
  }, 3000);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="output"></div>
mkrieger1
  • 19,194
  • 5
  • 54
  • 65
John Mark
  • 129
  • 1
  • 15

4 Answers4

1

I suggest using setTimeout to wait for fadeOut to finish the animation.

count = 10;
setTimeout(function onTimeout(){
  // log value of count
  console.log(count);

   $('.output').append(
    " hello world"+count+"<br>"
    ).hide().fadeIn(1000).delay(2000).fadeOut('slow', function() {
      if(count > 0) {
        count--;
        setTimeout(onTimeout, 3000);
      } 
    });
}, 3000);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="output"></div>
Bsalex
  • 2,847
  • 1
  • 15
  • 22
0

Use a recursive call to a function when last animation completes instead. It's very difficult to synchronize setTinterval with a series of animations and not have one get ahead of the other and the difference gets more exaggerated over time

Something like:

var delay =1000;//speeded up dfor demo
var count = 10;
// store setInterval ID in var
function counter() {
  // log value of count
  console.log(count);

  $('.output').append(
    " hello world" + count + "<br>"
  ).hide().fadeIn(1000).delay(2000).fadeOut('slow', function() {
    // fadeOut finished, decide to do it again or not
    if (count > 0) {
      count--;
      // call function again          
      setTimeout(counter,delay);
    }
  });
}

setTimeout(counter,delay)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="output"></div>
charlietfl
  • 170,828
  • 13
  • 121
  • 150
0

The best to achieve your goal is to use setTimeout instead of setInterval. You can use it like this way :

<script>


//alert("js");

function fadeOut()
{
    alert('do fadeout here');
}

function hello()
{
    alert('say hello word');

    setTimeout(fadeOut,2000);
}

function first()
{
    setTimeout(hello,3000);
}

//setTimeout(null,3000);
setTimeout(first(), 3000)  ;  
//alert('hello');

</script>

Hope it helps.

0

this is my solution. thank you @chcharlietfl

count = 10;
setTimeout(function onTimeout(){
  // log value of count
  console.log(count);

   $('.output').append(
    " hello world"+count+"<br>"
    ).hide().fadeIn(1000).delay(2000).fadeOut('slow', function() {
      if(count > 0) {
        count--;
        setTimeout(onTimeout, 3000);
      } 
    });
}, 3000);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="output"></div>
John Mark
  • 129
  • 1
  • 15