First, the event object

A. event.currentTarget

Example:

Current in the event bubbling phase
The element

$(“p”).click(function(event) {

alert( event.currentTarget === this ); // true

});

B. event.which

Description:

For keyboard and mouse events, this property determines which key or button you are pressing.

event.which

will
event.keyCode
and
event.charCode
Standardized. Recommend to use
event.which
To monitor keyboard input

Example:

<! 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])

An overview of the

Call a function or array function when the delay succeeds

The argument can be a function or an array of functions. When the delay succeeds,
Is invoked. Callbacks are executed in the order they were added. Once the
deferred.done()
Return the delayed object. Other methods of the delayed object can also be linked here, including increment
.done()
Methods. When the delay is resolved,
doneCallbacks
Execute using arguments provided to

resolve

or

resolveWith

Methods are called in the order they are added. For more information, see

Deferred object

.

parameter

DoneCallbacksFunction/ArrayV1.5

A function or array function that is deferred on success

DoneCallbacksFunction/ArrayV1.5

Appends optional functions or array functions, deferred on success

The sample

describe

Once the
Method returns an object from the deferred object
jqXHR
Object that we can attach a successful callback to use
.done
Methods.

jQuery

code
:

$.get(“test.php”).done(function() {

alert(“$.get succeeded”);

});

D. Objectdeferred.fail(failCallbacks[,failCallbacks])

An overview of the

Call a function or array function when delay fails
.

The argument can be a function or an array of functions. When the delay fails,
Is invoked. Callbacks are executed in the order they were added. Once the
deferred.fail()
Return the delayed object. Other methods of the delayed object can also be linked here, including increment
.done()
Methods. When the delay is resolved,
doneCallbacks
Execute using arguments provided to

resolve

or

resolveWith

Methods are called in the order they are added. For more information, see

Deferred object

.

parameter

FailCallbacksFunction/ArrayV1.5

A function or array function called on deferred failure

FailCallbacksFunction/ArrayV1.5

Appends optional functions or array functions, deferred to be called on failure

The sample

describe

Once the
Method returns a
jqXHR
The object, which is derived from a deferred income, can be used to attach success and failure callbacks
deferrred.done()
and
deferred.fail()
Methods.

jQuery

code
:

$.get(“test.php”)

.done(function(){ alert(“$.get succeeded”); / /})

Delay success

.fail(function(){ alert(“$.get failed!” ); }); //

Delayed failure

E. deferred.promise([type],[target])

An overview of the

Returns a
The object is used to see if the queuing is complete when all actions of a certain type are bound to the collection.

.promise()

Method returns a dynamically generated
Promise
The object is used to see if the queuing is complete when all actions of a certain type are bound to the collection.

By default,
is
“fx”
This means that all animations are returned for selected elements that have been completed
Promise
It’s solved.

Resolve the context and the only parameter is which collection to
Is invoked.

if
Is to provide,
.promise()
Will append to its method and then return this object instead of creating a new one. This pair appends to an already existing object
Promise
Is very useful.

parameter

typeStringV1.6

String to process

targetObjectV1.5

attached
methods
Object

The sample

describe

On a collection
And there is no animation to solve it
promise
.

jQuery

code
:

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])

An overview of the

The add handler is called when the deferred object is resolved or rejected.

parameter

alwaysCallbacksV1.6

A function, or array of functions, is called when the deferred object is resolved or rejected.

alwaysCallbacksV1.6

Appends an optional function, or array of functions, to be called when the deferred object is resolved or rejected.

The sample

describe

jQuery.get

The () method returns a value from a delayed object
jqXHR
Object that we can attach a success and error to use
deferred.always
() method callback.

jQuery

code
:

$.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)

Can be used to deal with multiple items that have sequencing or dependencies
request

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

As mentioned above,
The object allows you to specify a callback function for multiple events, which is not possible in traditional writing. Take a look at the code below, which uses a new approach

$.when()

:

$.when($.ajax(“test1.html”), $.ajax(“test2.html”))

.done(function(){ alert(“

Haha, it worked!
“); })

.fail(function(){ alert(“

Error!
“); });

What this code means is that you do two things first
and
$.ajax(“test2.html”)
If all successful, run
done()
The specified callback function; If one or both fail, execute
fail()
The specified callback function.

$.when()

Can only be
deferred
Object, if it is not
done
Will execute immediately:

var wait = function(){

var tasks = function(){

alert(“

Execution complete!
“);

};

setTimeout(tasks,5000);

};

$.when(wait())

.done(function(){ alert(“

Haha, it worked!
“); })

.fail(function(){ alert(“

Error!
“); });

References:

Blog.csdn.net/ssisse/arti…

https://www.jquery123.com/category/deferred-object/