将参数传递给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?
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您无需执行任何特殊操作即可访问
setTimeout
中的lastNumber
。但是,我认为您的意思是在
setTimeout
中使用num
而不是lastNumber
。You shouldn't have to do anything special to be able to access
lastNumber
in thesetTimeout
.However, I think you mean to use
num
instead oflastNumber
inside thesetTimeout
.