设定事件间隔和延迟

<script type="text/javascript">
var t=self.setInterval("clock()", 50);
function clock(){
    var time=new Date();
    document.getElementById("clock").value=time;
}

function timeMsg(){
    iSecond=Math.ceil((Math.random()*10));
    var t=setTimeout("log()", iSecond*1000);
}
function log(){
    $("#log").html("时间延迟:"+iSecond+"seconds!")
}
</script>
</head>
<body>
    <h2>jQuery设定时间间隔的方法</h2>
    <input type="text" id="clock" size="36"/>
    <button onclick="int=window.clearInterval(t)">stop interval</button>

    <h2>jQuery设定时间延迟的方法</h2>
    <form>
        <input type="button" value="Displayed timed alertbox!" onclick="timeMsg()"/>
    </form>
    <p>Click on the button above.An alert box will be displayed after 5 seconds.</p>
    <div id="log"></div>
</body>

setInterval(code,millisec[,”lang”]);
当中code參数表示要调用的函数或要运行的代码串,millisec參数表示周期性运行或调用code之间以毫秒计的时间间隔
setTimeout(code,millisec[,”lang”])
当中code參数表示要调用的函数或要运行的代码串,millisec參数表示在运行前需等待的毫秒数,code仅仅运行一次。若要多次调用,必须使用setInterval()或让code自身再次调用setTimeout()函数

原文地址:https://www.cnblogs.com/bhlsheji/p/5157123.html