js实现php中sleep()延时的功能
1.?jquery的$.delay()方法
設置一個延時來推遲執行隊列中之后的項目。這個方法不能取代JS原生的setTimeout。
The?.delay()?method?is?best?for?delaying?between?queued?jQuery?effects.?Because?it?is?limited—it?doesn't,?for?example,?offer?a?way?to?cancel?the?delay—.delay()?is?not?a?replacement?for?JavaScript's?native?setTimeout?function,?which?may?be?more?appropriate?for?certain?use?cases.
例子:在.slideUp()?和?.fadeIn()之間延時800毫秒。
HTML?代碼:
<div?id="foo?/">
jQuery?代碼:
$('#foo').slideUp(300).delay(800).fadeIn(400);
2. 通過循環消耗cpu
????function?sleep(n)?{
????var?start?=?new?Date().getTime();
????while(true)??if(new?Date().getTime()-start?>?n)?break;
????}
3.?用setTimeout。
假設有三個步驟,步驟之間需要暫停一段時間;可以采用如下的方法:
function?firstStep()?{
//do?something
setTimeout("secondStep()",?1000);
}
function?secondStep()?{
//do?something
setTimeout("thirdStep()",?1000);
}
function?thirdStep()?{
//do?something
}
總結
以上是生活随笔為你收集整理的js实现php中sleep()延时的功能的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: cmake--gccxml
- 下一篇: 动态规划算法php,php算法学习之动态