javascript中延时弹出窗口问题

mearer 2013-08-02 08:14:17
<script type="text/javascript">
function showWindow1(){
setTimeout(alert("hehe"),10000);
}
</script>
我写的这行代码,目的是为了当触发这个动作时,alert窗口能延时10秒才弹出,但是我运行时发现这并不是延迟10秒弹出,而是一触发就弹出了alert窗口,这是什么原因啊?该怎么改这个代码才能让alert窗口延迟10秒弹出啊?
...全文
708 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
mearer 2013-08-02
  • 打赏
  • 举报
回复 1
引用 6 楼 xiaofanku 的回复:
setTimeout("alert(‘hehe’)",10000);
原来是单引号的问题,谢谢了!
Rommel_Gao 2013-08-02
  • 打赏
  • 举报
回复
6楼正解。 如果是这种写法,所有浏览器都应该支持。至于IE6这种奇葩的浏览器版本。。。 理论上浏览器都支持这两种写法: window.setTimeout(func, delay, [param1, param2, ...]); window.setTimeout(code, delay); 但是基本都不推荐第二种用法,原因和eval()差不多吧,可以参考各个浏览器对于setTimeout的解释 https://developer.mozilla.org/en-US/docs/Web/API/window.setTimeout http://msdn.microsoft.com/en-us/library/ie/aa741500%28v=vs.85%29.aspx 因此,还是建议你采用2楼,3楼的建议吧。
街头小贩 2013-08-02
  • 打赏
  • 举报
回复
setTimeout("alert(‘hehe’)",10000);
mearer 2013-08-02
  • 打赏
  • 举报
回复
引用 3 楼 xiaofanku 的回复:
或者

<script type="text/javascript">
function showWindow1(){
	this.tip=function(){alert("hehe");}
	setTimeout(tip,1200);
}
showWindow1();
</script>
function showWindow1(){ setTimeout("alert("hehe")",10000); } //这样子写在IE6可以运行哦,没问题,但在IE9就不行了,这是什么原因呢?是不是浏览器的内核问题啊,还是语言的问题?
mearer 2013-08-02
  • 打赏
  • 举报
回复
引用 1 楼 fzfei2 的回复:
function showWindow1(){ setTimeout(function(){ alert("hehe") } ,10000); }
<script type="text/javascript"> function showWindow1(){ setTimeout("alert("hehe")",10000); } //这样子写在IE6可以运行哦,没问题,但在IE9就不行了,这是什么原因呢?是不是浏览器的内核问题啊,还是语言的问题?
街头小贩 2013-08-02
  • 打赏
  • 举报
回复
或者

<script type="text/javascript">
function showWindow1(){
	this.tip=function(){alert("hehe");}
	setTimeout(tip,1200);
}
showWindow1();
</script>
街头小贩 2013-08-02
  • 打赏
  • 举报
回复

<script type="text/javascript">
setTimeout(function(){
	alert('hello world');
	},1200);
</script>
fzfei2 2013-08-02
  • 打赏
  • 举报
回复
function showWindow1(){ setTimeout(function(){ alert("hehe") } ,10000); }

87,904

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 JavaScript
社区管理员
  • JavaScript
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧