jquery delay the execution of code
在我的jquery函数中,我有一个加载器gif图像。 显示完之后,我想延迟一秒钟,然后继续执行其余代码。
我怎样才能做到这一点?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | $('#loader').css('display', ''); //// I want to put here a delay. var myDate = new Date(); myDate.setFullYear(2013,8,2); var checkyear = myDate.getFullYear(); var monthly =myDate.getMonth(); var daily =myDate.getDate(); $('#day').html(daily) ; $('#month').html(months[monthly]) ; $('#year').html(checkyear) ; |
设置这样的超时:
1 2 3 4 | var delay = 1000; setTimeout(function() { // your code }, delay); |
例
http://jsfiddle.net/HuLTs/
您尝试过.delay吗?
1 | $('#loader').show(1).delay(1000).hide(1); |
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.
演示:http://jsfiddle.net/SBrWa/
1 2 3 4 5 6 | $(document).ready(function(){ setTimeout(function(){ //your code }, 2000); }); |
这里
试试这个,
1 2 3 4 5 6 7 8 9 10 11 12 13 | $(function(){ $('#loader').css('display', ''); setTimeout(function(){ var myDate = new Date(); myDate.setFullYear(2013,8,2); var checkyear = myDate.getFullYear(); var monthly =myDate.getMonth(); var daily =myDate.getDate(); $('#day').html(daily) ; $('#month').html(months[monthly]) ; $('#year').html(checkyear) ; },1000);// 1 second delay }); |
您可以使用此代码,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | $('#loader').css('display', ''); setTimeout(function() { var myDate = new Date(); myDate.setFullYear(2013,8,2); var checkyear = myDate.getFullYear(); var monthly =myDate.getMonth(); var daily =myDate.getDate(); $('#day').html(daily) ; $('#month').html(months[monthly]) ; $('#year').html(checkyear) ; }, 1000); |