平台严格禁止发布违法/不实/欺诈等垃圾信息,一经发现将永久封禁帐号,针对违法信息将保留相关证据配合公安机关调查!
有些朋友对SetInterval和setTimeout用法不是很了解,今天简单介绍一下,以下信息来自网络!请自行测试!
JS里设定延时:
<script language="JavaScript" type="text/javascript"> <!-- function hello(){ alert("hello"); } window.setTimeout(hello,5000); //--> </script>
<script language="JavaScript" type="text/javascript"> <!-- function hello(){ alert("hello"); } var id=window.setTimeout(hello,5000); document.onclick=function(){ window.clearTimeout(id); } //--> </script>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <html> <head> <title> New Document </title> </head> <body> <form action="somepage.asp"> <input type="text" value="0" name="txt1"/> <input type="button" value="开始" name="btnStart"/> <input type="button" value="重置" name="btnReset"/> </form> </body> </html> <script language="JavaScript" type="text/javascript"> <!-- //获取表单中的表单域 var txt=document.forms[0].elements["txt1"]; var btnStart=document.forms[0].elements["btnStart"]; var btnReset=document.forms[0].elements["btnReset"] //定义定时器的id var id; //每10毫秒该值增加1 var seed=0; btnStart.onclick=function(){ //根据按钮文本来判断当前操作 if(this.value=="开始"){ //使按钮文本变为停止 this.value="停止"; //使重置按钮不可用 btnReset.disabled=true; //设置定时器,每0.01s跳一次 id=window.setInterval(tip,10); } else{ //使按钮文本变为开始 this.value="开始"; //使重置按钮可用 btnReset.disabled=false; //取消定时 window.clearInterval(id); } } //重置按钮 btnReset.onclick=function(){ seed=0; } //让秒表跳一格 function tip(){ seed++; txt.value=seed/100; } //--> </script>
function hello(_name){ alert("hello,"+_name); }
window.setTimeout("hello(userName)",3000);
<script language="JavaScript" type="text/javascript"> <!-- var userName="jack"; //根据用户名显示欢迎信息 function hello(_name){ alert("hello,"+_name); }
//创建一个函数,用于返回一个无参数函数 function _hello(_name){ return function(){ hello(_name); } } window.setTimeout(_hello(userName),3000); //--> </script>
例如:
tttt=setTimeout('northsnow()',1000); clearTimeout(tttt);
tttt=setInterval('northsnow()',1000); clearInteval(tttt);
<div id="liujincai"> </div> <input type="button" name="start" value="start" onclick='startShow();'> <input type="button" name="stop" value="stop" onclick="stop();"> <script language="javascript"> var intvalue=1; var timer2=null; function startShow() { liujincai.innerHTML=liujincai.innerHTML + " " + (intvalue ++).toString(); timer2=window.setTimeout("startShow()",2000); } function stop() { window.clearTimeout(timer2); } </script>
<div id="liujincai"> </div> <input type="button" name="start" value="start" onclick='timer2=window.setInterval("startShow()",2000);//startShow();'> <input type="button" name="stop" value="stop" onclick="stop();"> <script language="javascript"> var intvalue=1; var timer2=null; function startShow() { liujincai.innerHTML=liujincai.innerHTML + " " + (intvalue ++).toString(); } function stop() { window.clearInterval(timer2); } </script>以上信息来自网络!请自行测试!