Skip to content

Instantly share code, notes, and snippets.

@nuysoft
Last active December 20, 2015 05:19
Show Gist options
  • Save nuysoft/6077974 to your computer and use it in GitHub Desktop.
Save nuysoft/6077974 to your computer and use it in GitHub Desktop.
队列 Queue - 如何使用: 事实上,队列模块可以应用于任何需要顺序执行函数的场景。下面是几个典型示例。
<div id="ajaxqueue"></div>
<script>
$('#ajaxqueue')
.queue( 'ajax', function( next ){
var $this = $(this).append('$.ajax 1 loading...');
$.ajax( '/queue.do?method=queue1', { dataType: "json" } )
.done( function(){ $this.append('done!<br>'); })
.done( function(){ next(); } ) // 请求成功后执行下一个
.fail( function(){ $this.append('fail!<br>'); });
} )
.delay( 1000, 'ajax' )
.queue( 'ajax', function( next ){
var $this = $(this).append('$.ajax 2 loading...');
$.ajax( '/queue.do?method=queue2', { dataType: "json" } )
.done( function(){ $this.append('done!<br>'); })
.done( function(){ next(); } ) // 请求成功后执行下一个
.fail( function(){ $this.append('fail!<br>'); });
} )
.delay( 1000, 'ajax' )
.queue( 'ajax', function( next ){
var $this = $(this).append('$.ajax 3 loading...');
$.ajax( '/queue.do?method=queue3', { dataType: "json" } )
.done( function(){ $this.append('done!<br>'); })
.done( function(){ next(); } ) // 请求成功后执行下一个
.fail( function(){ $this.append('fail!<br>'); });
} )
.dequeue( 'ajax' ); // 手动出队并执行第一个函数
</script>
<div id="fxqueue" style="width: 200px; height: 50px; border: 1px solid gray; display: none; "></div>
<script>
$('#fxqueue')
.fadeIn() // 淡入动画,自动出队并执行
.delay(3000) // 等待 3000 毫秒
.queue(function( next ){ // 发起 AJAX 请求
var $this = $(this).append('$.ajax loading...');
$.ajax( '/queue.do?method=queue1', { dataType: "json" } )
.done( function(){ $this.append('done!<br>'); })
.done( function(data){ $this.append( JSON.stringify( data ) ); } )
.done( function(){ next(); } );
.fail( function(){ $this.append('fail!<br>'); })
} )
.delay(3000) // 等待 3000 毫秒
.fadeOut(); // 淡出动画
</script>
$({})
.queue('confirm', function( next ){
if( confirm( '确认?' ) ) next();
else alert('已取消')
})
.queue('confirm', function( next ){
if( confirm( '再次确认?' ) ) next();
else alert('已取消')
})
.queue('confirm', function( next ){
// do something
alert( '完成' );
})
.dequeue( 'confirm' ); // 手动出队并执行第一个函数
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment