First, the event object
A. event.currentTarget
$(“p”).click(function(event) {
alert( event.currentTarget === this ); // true
});
B. event.which
event.which
<! DOCTYPE html>
<html>
<head>
<script src=”http://code.jquery.com/jquery-latest.min.js”></script>
</head>
<body>
<input id=”whichkey” value=”type something”>
<div id=”log”></div>
<script>
$(‘#whichkey’).bind(‘keydown’,function(e){
$(‘#log’).html(e.type + ‘: ‘ + e.which ); });
</script>
</body>
</html>
Listen for changes in JS variables
Third, the delay object
C. deferred.done(doneCallbacks[,doneCallbacks])
resolve
resolveWith
Deferred object
DoneCallbacksFunction/ArrayV1.5
DoneCallbacksFunction/ArrayV1.5
jQuery
$.get(“test.php”).done(function() {
alert(“$.get succeeded”);
});
D. Objectdeferred.fail(failCallbacks[,failCallbacks])
resolve
resolveWith
Deferred object
FailCallbacksFunction/ArrayV1.5
FailCallbacksFunction/ArrayV1.5
jQuery
$.get(“test.php”)
.done(function(){ alert(“$.get succeeded”); / /})
.fail(function(){ alert(“$.get failed!” ); }); //
E. deferred.promise([type],[target])
.promise()
typeStringV1.6
targetObjectV1.5
jQuery
var div = $( “<div />” );
div.promise().done(function( arg1 ) {
// will fire right away and alert “true”
alert( this === div && arg1 === div );
F. deferred.always(alwaysCallbacks,[alwaysCallbacks])
alwaysCallbacksV1.6
alwaysCallbacksV1.6
jQuery.get
jQuery
$.get(“test.php”).always( function() {
alert(“$.get completed with success or error callback arguments”);
});
Four, the application
A. Tangled request sequencing problem (non-blocking resolution)
var promise = $.ajax(“/myServerScript1”);
function getStuff() {
return $.ajax(“/myServerScript2”);
}
promise.then(getStuff).then(function(myServerScript2Data){
// Do something
});
B. Specify a callback function for multiple operations
$.when()
$.when($.ajax(“test1.html”), $.ajax(“test2.html”))
.done(function(){ alert(“
.fail(function(){ alert(“
$.when()
var wait = function(){
var tasks = function(){
alert(“
};
setTimeout(tasks,5000);
};
$.when(wait())
.done(function(){ alert(“
.fail(function(){ alert(“
https://www.jquery123.com/category/deferred-object/