передача параметров в setTimeout? - PullRequest
1 голос
/ 29 марта 2011

В настоящее время я использую

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);
  });
}

Однако lastNumber всегда неопределен при повторном вызове функции, что мне нужно сделать, чтобы определить lastNumber?

1 Ответ

2 голосов
/ 29 марта 2011

Вам не нужно делать ничего особенного, чтобы иметь доступ к lastNumber в setTimeout.

Однако, я думаю, вы хотите использовать num вместо lastNumber внутри setTimeout.

...