Home  >  Article  >  Web Front-end  >  Analysis of the asynchronous mechanism of jQuery source code

Analysis of the asynchronous mechanism of jQuery source code

不言
不言Original
2018-07-09 15:26:581327browse

This article mainly introduces the analysis of the asynchronous mechanism of jQuery source code. It has certain reference value. Now I share it with everyone. Friends in need can refer to it

Asynchronous mechanism

JavaScript programming is often accompanied by asynchronous programming, such as remote data acquisition. A large amount of asynchronous programming will bring many callback functions. JS is single-threaded, so completing asynchronous often requires the help of browser event drivers, which will make our code and Algorithms become fragmented. jQuery provides an abstract non-blocking solution: Deferred.

Understanding asynchronous

alert(1)
setTimeout(function(){
    alert(2)
},0)
alert(3)
//alert(1) 
//alert(3)
//alert(2)

1. Understanding deferredObjects

In the process of developing websites, we often encounter certain JavaScript operations that take a long time. Among them, there are both asynchronous operations (such as ajax reading server data) and synchronous operations (such as traversing a large array) . None of them can get the results immediately.

The usual approach is to specify callback functions (callback) for them. That is, specify in advance which functions should be called once they have finished running.

deferred The object was introduced in jquery version 1.5. jquery was not very good at handling callbacks, so the deferred object was born. The deferred object is the callback function solution for jQuery. In English, defer means "delay", so the meaning of a deferred object is to "delay" execution until a certain point in the future.

2. Ajax chain operation

The initial writing method is:

// < 1.5
$.ajax({

    url: "test.html",

    success: function(){
      alert("哈哈,成功了!");
    },

    error:function(){
      alert("出错啦!");
    }

  });

If the version of jquery is 1.5 or below , then the XHR object is returned, so the chain operation cannot be performed. However, if the version is higher than 1.5, the Deferred object is returned, and the chain operation can be performed.

// >=1.5
$.ajax("test.html")
    .done(function(){
        
    })
    .fail(function(){
        
    })

done() is equivalent to the success method, and fail() is equivalent to the error method. After adopting the chain writing method, the readability of the code is greatly improved.

3. Specify multiple callback functions for the same operation

One of the great benefits of the deferred object is that it allows you to add multiple callback functions freely.

Still taking the above code as an example, if after the ajax operation is successful, in addition to the original callback function, I also want to run another callback function, what should I do?

$.ajax("test.html")

  .done(function(){ alert("哈哈,成功了!");} )

  .fail(function(){ alert("出错啦!"); } )

  .done(function(){ alert("第二个回调函数!");} );

4. Specify callback functions for multiple operations

Another great benefit of the deferred object is that it allows you to specify a callback function for multiple events, which is not possible with traditional writing. of. That is $.when()

$.when($.ajax("test1.html"), $.ajax("test2.html"))

  .done(function(){ alert("哈哈,成功了!"); })

  .fail(function(){ alert("出错啦!"); });
//$.ajax("test1.html")和$.ajax("test2.html"),如果都成功了,就运行done()指定的回调函数;如果有一个失败或都失败了,就执行fail()指定的回调函数。

5. Callback function interface extension for common operations

It extends this set of callback function interfaces from ajax operations to all operations. In other words, any operation - whether it is an ajax operation or a local operation, whether it is an asynchronous operation or a synchronous operation - can use various methods of the deferred object to specify a callback function.

// 一个耗时操作
var wait = function(){
    var task = function(){
        alert('执行了');
    };
    setTimeout(5000);
}

When we add a callback, we may think of $.when();

$.when(wait())

  .done(function(){ alert("哈哈,成功了!"); })

  .fail(function(){ alert("出错啦!"); });

But it has no effect. done is executed immediately because $.when() accepts a deferred object. So let’s improve it further

var dtd = $.Deferred();
var wait = function(){
    var task = function(){
        alert('执行了');
        dtd.resolve(); // 改变deferred对象的执行状态
    };
    setTimeout(task, 5000);

    return dd;
}

In this way, wait() returns a defferred object, which can be operated on the chain.

$.when(wait())

  .done(function(){ alert("哈哈,成功了!"); })

  .fail(function(){ alert("出错啦!"); });

6. defferred.resolve() and defferred.reject()

deferred uses the principle of Promise. Here we need to be clear about the concept of execution status. In the defferred object, there are three states, Uncompleted Completed Failed

If the execution status is "Completed " (resolved), the deferred object immediately calls the done() method specified callback function;
If the execution status is "Failed", call fail ()The callback function specified by the method;
If the execution status is "Not Completed", continue to wait, or call the callback function specified by the progress() method ( Added in jQuery 1.7 version).

defferred.resolve() is to change the status from Uncompleted to Completed. ==> done()
defferred.reject() changes the status from Uncompleted to Failed. ==> fail()

var dtd = $.Deferred(); // 新建一个Deferred对象
    var wait = function(){

    var tasks = function(){

      alert("执行完毕!");

      dtd.resolve(); // 改变Deferred对象的执行状态

    };

    setTimeout(tasks,5000);

    return dtd; // 返回promise对象

  };



  $.when(wait())

  .done(function(){ alert("哈哈,成功了!"); })

  .fail(function(){ alert("出错啦!"); });

  dtd.resolve();// 这里会立即先执行一次

7. defferred.promise();

The above method has some problems. For example, dtd is a global variable, and it can be used externally at any time. This edge state will cause done() or fail() to be executed more than once.

This is obviously not what we want, so we need the help of defferred.promise.

Its function is to return another deferred object on the original deferred object. The latter only opens methods unrelated to changing the execution status (such as done() method and fail() method), shielding and changing Execution status-related methods (such as resolve() method and reject() method), so that the execution status cannot be changed.
var wait = function(dtd){

    var dtd = $.Deferred(); //在函数内部,新建一个Deferred对象

    var tasks = function(){

      alert("执行完毕!");

      dtd.resolve(); // 改变Deferred对象的执行状态

    };

    setTimeout(tasks,5000);

    return dtd.promise(); // 返回promise对象

  };

    var dd = wait();
    // dd.resolve(); // 这里如果使用了的话,就会报错的哦,因为返回deferred.promise()是无法对状态修改的哦
  $.when(dd)

  .done(function(){ alert("哈哈,成功了!"); })

  .fail(function(){ alert("出错啦!"); });

8. $.Defferred(wait)

Another way to prevent execution status changes is to use $.Defferred()

var dtd = $.Deferred(); // 新建一个Deferred对象

  var wait = function(dtd){

    var tasks = function(){

      alert("执行完毕!");

      dtd.resolve(); // 改变Deferred对象的执行状态

    };

    setTimeout(tasks,5000);

    return dtd;

  };
var ddd = wait();
// dd.resolve(); //这里报错了哦
$.Deferred(dd)

  .done(function(){ alert("哈哈,成功了!"); })

  .fail(function(){ alert("出错啦!"); });

9. Summary of defferred objects

  • $.Deferred() generates a deferred object.

  • deferred.done() specifies the callback function when the operation is successful

  • deferred.fail() 指定操作失败时的回调函数

  • deferred.promise() 没有参数时,返回一个新的deferred对象,该对象的运行状态无法被改变;接受参数时,作用为在参数对象上部署deferred接口。

  • deferred.resolve(args) 手动改变deferred对象的运行状态为"已完成",从而立即触发done()方法。解决Deferred(延迟)对象,并根据给定的args参数调用任何完成回调函数(doneCallbacks)。

  • deferred.resolveWith()    解决Deferred(延迟)对象,并根据给定的 context和args参数调用任何完成回调函数(doneCallbacks)。

  • deferred.reject(args) 这个方法与deferred.resolve()正好相反,调用后将deferred对象的运行状态变为"已失败",从而立即触发fail()方法。拒绝Deferred(延迟)对象,并根据给定的args参数调用任何失败回调函数(failCallbacks)。这里的args是一个对象。

  • deferred.rejectWith(context, args)  拒绝Deferred(延迟)对象,并根据给定的 context和args参数调用任何失败回调函数(failCallbacks)。这里的args是一个数组类型。

  • $.when() 为多个操作指定回调函数。除了这些方法以外,deferred对象还有二个重要方法,上面的教程中没有涉及到。Context(上下文) 作为 this对象传递给失败回调函数(failCallbacks )

  • deferred.then()

有时为了省事,可以把done()和fail()合在一起写,这就是then()方法。

$.when($.ajax( "/main.php" ))

  .then(successFunc, failureFunc );

如果then()有两个参数,那么第一个参数是done()方法的回调函数,第二个参数是fail()方法的回调方法。如果then()只有一个参数,那么等同于done()。

  • deferred.always() 这个方法也是用来指定回调函数的,它的作用是,不管调用的 deferred.resolve()还`是deferred.reject(),最后总是执行。

$.ajax( "test.html" )

  .always( function() { alert("已执行!");} );
  • deferred.state() 确定一个Deferred(延迟)对象的当前状态。

  1. "pending": Deferred对象是尚未完成状态 (不是 "rejected" 或 "resolved").

  2. "resolved": Deferred对象是在解决状态,这意味着,deferred.resolve() 或者 deferred.resolveWith()被对象访问和doneCallbacks被访问(或在被调用的过程中)

  3. "rejected": Deferred对象是在被拒绝的状态,这意味着,deferred.reject() 或者 deferred.rejectWith() 被对象访问和failCallbacks被访问(或在被调用的过程中) 。

这种方法主要是用在调试,例如,在准备拒绝(reject)一个延迟对象前,判断它是否已经处于 resolved 状态。


十、使用实例

情景1:当用户按下删除弹窗的确定或取消后,把弹窗隐藏,并执行对应的操作(删除或不执行),因为我们不知道用户什么时候会点击按钮,所以不能让弹窗阻塞其他任务的执行。

function pop(arg) {
      if (!arg) {
        console.error('pop title is empty');
      }
      var dfd = $.Deferred() //实例化一个延迟对象 
        ,
        confirmed //记录按下确定或取消 
        , $confirm = $content.find('button.confirm') //确认按钮 
        ,
        $cancel = $content.find('button.cancel'); //取消按钮 

      //定时器轮询,当按下确定或取消时触发删除或取消操作 
      timer = setInterval(function() {
        if (confirmed !== undifined) {
          dfd.resolve(confirmed);
          clearInterval(timer);
          dismiss_pop();
        }
      }, 50);
      //点击确定时更改confirmed状态 
      $confirm.on('click', function() {
        confirmed = true;
      });
      //点击取消时更改confirmed状态 
      $cancel.on('click', function() {
        confirmed = false;
      }); //返回dfd对象
      return dfd.promise();
    }
    $('.delete').click(function() {
      var $this = $(this);
      var index = $this.data('index');
      //当前的id //确定删除
      pop('确定删除?').then(function(res) {
        res ? delete_task(index) : null;
      })
    })

以上就是本文的全部内容,希望对大家的学习有所帮助,更多相关内容请关注PHP中文网!

相关推荐:

The above is the detailed content of Analysis of the asynchronous mechanism of jQuery source code. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn