将参数传递给setTimeout?

发布于 2024-10-27 16:35:40 字数 829 浏览 5 评论 0原文

目前我正在使用

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?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

月牙弯弯 2024-11-03 16:35:40

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

但是,我认为您的意思是在 setTimeout 中使用 num 而不是 lastNumber

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文