js如何实现等待1秒后执行一个事件

JavaScript011

js如何实现等待1秒后执行一个事件,第1张

需要准备的材料分别有:电脑、html编辑器、浏览器。

1、首先,打开html编辑器,新建html文件,例如:index.html。

2、在index.html中的<body>标签中输入:<button onclick="setTimeout(send, 1000)">btn</button>,<script>标签中输入js代码:function send() {document.body.innerText = 'use send'}。

3、浏览器运行index.html页面,点击btn按钮。

4、等待1秒后,确实执行了send()方法打印了“use send”文本。

JS:

setTimeout("这里写你等待1秒后执行的函数名",1000)

例:

function showHello()

{

document.write("你好!")

}

setTimeout("showHello()",1000)

可以这样:

var menu=getid("menu")

var li=gettag("li",menu)

for(var i=0i<li.lengthi++){

li[i].onmouseover=function(){

if(this.className.indexOf("show")<0)

{

setTimeout("showHello(this)",1000)

}

}

li[i].onmouseout=function(){

$(this).removeClass("show")

}

}

function showHello(obj)

{

$(obj).addClass("show")

}