【问题标题】:Cannot remove the State of Defer Object无法移除延迟对象的状态
【发布时间】:2014-01-07 10:09:19
【问题描述】:
 var defer8 = $.Deferred();
    function get_outlet_data()
    {
        db.transaction(populateDBOutlet);
        retrieve_Data_outlet(localStorage.username);
     defer8.resolve();
    }


$.when(defer8).then(function() {
 });

这里我使用 defer 对象等到函数 get_outlet_data 运行。但是我需要删除 When 函数中的 defer8 状态

【问题讨论】:

标签: jquery


【解决方案1】:
var get_outlet_data = function () {
    var deferred = $.Deferred();
    db.transaction(populateDBOutlet);
    retrieve_Data_outlet(localStorage.username);
    deferred.resolve(); // Note: the lines above are probably asynchronous, so you want to resolve the promise when they are done
    return deferred.promise();
};

$.when(get_outlet_data()).then(function () {
    // No need to remove the state, because the deferred will be re-created on a second call
});

我所做的是将延迟对象移动到函数内,因此每次调用函数时都会重新创建它。因此应该没有必要“重置状态”(我认为这是不可能的)

编辑:正如您所知,您的代码中有一个错误: $.when 不接受延迟对象,它接受了一个承诺。所以正确的使用方式是 $.when(deferred.promise(), ...) 而不是 $.when(deferred, ...)

【讨论】:

  • $.when 与延迟对象一起使用。无论如何,promise 接口是延迟对象的一部分。也就是说,你的答案是我能想到的唯一一个以某种方式适合 OP 问题的答案(如果我理解他的问题)。但是,这样,OP 仍然不能保持对延迟对象的引用
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-21
  • 1970-01-01
  • 2021-11-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多