Hailwood
Hailwood

Reputation: 92581

passing parameters into setTimeout?

Currently I am using

function showGrowl(lastNumber) {
  var num = lastNumber;
  //keep generating a random number untill it is not the same as the lastNumber used 
  while((num = Math.ceil(Math.random() * 3)) == lastNumber);

  //create a clone of the chosen notification
  var clone = $('.notification.n' + num).clone();

  //show the clone
  clone.appendTo('#contain').show('fast', function() {

    //10 seconds after showing, hide the notification
    setTimeout(function() {
      clone.hide('fast', function() {

        //once it is hidden remove it
        clone.remove();

        //then two seconds later show a new notification
        setTimeout(function() {
          showGrowl(lastNumber)
        }, 2000);
      })
    }, 10000);
  });
}

However lastNumber is always undefined when it calls the function again, what do I need to do so that lastNumber is defined?

Upvotes: 1

Views: 160

Answers (1)

David Tang
David Tang

Reputation: 93664

You shouldn't have to do anything special to be able to access lastNumber in the setTimeout.

However, I think you mean to use num instead of lastNumber inside the setTimeout.

Upvotes: 2

Related Questions