Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/472.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/backbone.js/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript jQuery:延迟/承诺_Javascript_Backbone.js - Fatal编程技术网

Javascript jQuery:延迟/承诺

Javascript jQuery:延迟/承诺,javascript,backbone.js,Javascript,Backbone.js,我试图从一个示例应用程序中找出主干,请参见。代码使用jQuery的延迟和承诺,正如您在下面的代码中看到的那样。我已经阅读了jQuery上的文档,但是我很难从下面的示例中了解这些方法是如何使用的。您可能需要更多的代码来回答这个问题,但可能不需要。这些是我关于它的问题 1完成淡出后是否调用dfd.resolve?如果是,dfd.resolve触发器是什么 2.通过回报诺言发生的事情;它正在调用延迟方法吗?什么时候为什么要这样做?这似乎是一种递归方法 3 dfd.resolve是否可能触发此代码中未显

我试图从一个示例应用程序中找出主干,请参见。代码使用jQuery的延迟和承诺,正如您在下面的代码中看到的那样。我已经阅读了jQuery上的文档,但是我很难从下面的示例中了解这些方法是如何使用的。您可能需要更多的代码来回答这个问题,但可能不需要。这些是我关于它的问题

1完成淡出后是否调用dfd.resolve?如果是,dfd.resolve触发器是什么

2.通过回报诺言发生的事情;它正在调用延迟方法吗?什么时候为什么要这样做?这似乎是一种递归方法

3 dfd.resolve是否可能触发此代码中未显示的其他方法

      hide: function() {
            if ((":visible") === false) {

                return null;

            }
            promise = $.Deferred(_.bind(function(dfd) { 
                this.el.fadeOut('fast', dfd.resolve)}, this));
            return promise.promise();
        },

        show: function() {
            if (this.el.is(':visible')) { 
                return;
            }       
            promise = $.Deferred(_.bind(function(dfd) { 
                console.log("in promise section of show in base view");
                this.el.fadeIn('fast', dfd.resolve) }, this))
            return promise.promise();
        }
1完成淡出后是否调用dfd.resolve?如果是的话,你会怎么做 解决触发器

对。将回调作为其参数之一。动画完成后,它将执行回调。在这种情况下,它恰好是延迟的解析方法

2.通过回报诺言发生的事情;是不是在打电话 延迟方法?什么时候为什么要这样做?这看起来像是一场灾难 递归方法

这里没有发生任何事情。promise只是一个变量,它包含对已创建的延迟对象的引用。是上的一个方法,该方法返回延迟的修改版本,该版本不允许您修改其行为方式。因此,调用方可以确保它将始终以相同的方式执行

3 dfd.resolve是否可能触发其他方法而不是 显示在这个代码中

      hide: function() {
            if ((":visible") === false) {

                return null;

            }
            promise = $.Deferred(_.bind(function(dfd) { 
                this.el.fadeOut('fast', dfd.resolve)}, this));
            return promise.promise();
        },

        show: function() {
            if (this.el.is(':visible')) { 
                return;
            }       
            promise = $.Deferred(_.bind(function(dfd) { 
                console.log("in promise section of show in base view");
                this.el.fadeIn('fast', dfd.resolve) }, this))
            return promise.promise();
        }
对。延迟对象只不过是一个允许您注册回调的对象。调用延迟将触发,而调用将触发任何

一个非常简单的例子可能如下所示:

//A Deferred takes in a function that will be passed a reference
// to the Deferred object. This allows you to resolve or reject
// it at some point in the future.
var promise = $.Deferred(function(def){

   setTimeout(function(){

      def.resolve('Five seconds have passed!');      

   }, 5000);

}).promise();

//This will only get executed when the
// underlying Deferred gets resolved
promise.done(function(msg){

   alert(msg); // Displays: 'Five seconds have passed!'

});
1完成淡出后是否调用dfd.resolve?如果是的话,你会怎么做 解决触发器

对。将回调作为其参数之一。动画完成后,它将执行回调。在这种情况下,它恰好是延迟的解析方法

2.通过回报诺言发生的事情;是不是在打电话 延迟方法?什么时候为什么要这样做?这看起来像是一场灾难 递归方法

这里没有发生任何事情。promise只是一个变量,它包含对已创建的延迟对象的引用。是上的一个方法,该方法返回延迟的修改版本,该版本不允许您修改其行为方式。因此,调用方可以确保它将始终以相同的方式执行

3 dfd.resolve是否可能触发其他方法而不是 显示在这个代码中

      hide: function() {
            if ((":visible") === false) {

                return null;

            }
            promise = $.Deferred(_.bind(function(dfd) { 
                this.el.fadeOut('fast', dfd.resolve)}, this));
            return promise.promise();
        },

        show: function() {
            if (this.el.is(':visible')) { 
                return;
            }       
            promise = $.Deferred(_.bind(function(dfd) { 
                console.log("in promise section of show in base view");
                this.el.fadeIn('fast', dfd.resolve) }, this))
            return promise.promise();
        }
对。延迟对象只不过是一个允许您注册回调的对象。调用延迟将触发,而调用将触发任何

一个非常简单的例子可能如下所示:

//A Deferred takes in a function that will be passed a reference
// to the Deferred object. This allows you to resolve or reject
// it at some point in the future.
var promise = $.Deferred(function(def){

   setTimeout(function(){

      def.resolve('Five seconds have passed!');      

   }, 5000);

}).promise();

//This will only get executed when the
// underlying Deferred gets resolved
promise.done(function(msg){

   alert(msg); // Displays: 'Five seconds have passed!'

});

谢谢,如果对延迟调用resolve将触发done处理程序,那么OP中代码中的done处理程序是什么?这就是我不明白的。dfd之后似乎什么也没有发生。resolve@user1647484-我还没有看到代码的其余部分,所以我不太清楚,但我的想法似乎是,您可以在视图上调用hide或show,并将回调链接到视图上,这样您就可以保证在动画完成后执行后续操作。谢谢,因此,如果对延迟处理调用resolve将触发done处理程序,那么OP中代码中的done处理程序是什么?这就是我不明白的。dfd之后似乎什么也没有发生。resolve@user1647484-我还没有看到其余的代码,所以我不知道,但我的想法似乎是,您将调用隐藏或显示视图,并将回调链接到视图上,这样您就可以保证在动画完成后执行后续操作。对于第二个问题,有关第二个问题,请参见