将参数传递给setTimeout?

时间:2011-03-29 01:50:14

标签: javascript jquery settimeout

目前我正在使用

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 个答案:

答案 0 :(得分:2)

您无需执行任何特殊操作即可访问lastNumber中的setTimeout

但是,我认为您的意思是在num内使用lastNumber代替setTimeout

相关问题