setTimeout 不适用于 window.location?
我尝试在更改窗口位置时实现丰富的闪光效果,但是有一个小问题,我无法解决。
请查看脚本
$(document).ready(function(){
$('a.flash').click(function(e) {
e.preventDefault();
$('body').fadeOut(1500);
setTimeout("", 1500);
window.location=this.href;
});
});
window.location=this.href
必须在 1500 毫秒后完成,但它没有发生。 你能解释一下为什么吗? 奇怪的是,当我尝试编写 alert("something");
而不是 window.location=this.href
时,它工作正常。你能解释一下为什么吗?
谢谢
i try to rich flash like effect when changing window location, but there is a small problem, i can't solve.
look at the script please
$(document).ready(function(){
$('a.flash').click(function(e) {
e.preventDefault();
$('body').fadeOut(1500);
setTimeout("", 1500);
window.location=this.href;
});
});
window.location=this.href
must be done after 1500ms, but it doesn't happen.
could you explain why?
what is strange, when i try to write alert("something");
instead of window.location=this.href
, it works fine. Could you explain why?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您应该提供一个回调函数作为 setTimeout 的第一个参数,该函数在 1500 毫秒后调用。
You're supposed to provide a callback function as the first param of setTimeout which is invoked after 1500 ms.
setTimeout
与其他语言中的Thread.sleep(1500);
不同。setTimeout
安排一段代码在将来的某个时刻运行,并且不会阻塞。执行立即传递setTimeout
调用并继续。第一个参数是对函数的引用或将要计算的字符串。
请参阅 meder 的答案,了解使用 setTimeout 的适当方法,避免使用匿名函数进行评估。
setTimeout
is not equivalent to aThread.sleep(1500);
in other languages.setTimeout
schedules a piece of code to be run at some point in the future and does not block. Execution immediately passes thesetTimeout
call and continues on.The first parameter is either a reference to a function or a string that will be evaluated.
See meder's answer for the appropriate way to use
setTimeout
, avoiding evaluation using an anonymous function.