- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试设置一个 Ajax 调用循环和一个在所有 Ajax 调用解决后运行的函数。我读了一堆关于这个问题的问题,并认为我能够将它应用到我的代码中,但它不起作用,我一生都无法弄清楚为什么。
这是 JS 代码。 map 函数
运行了 10 次(仅供引用)。
function addData(data) {
var deferreds = [];
$.map(data, function(job) {
deferreds.push(addLocation(job));
});
console.log(deferreds);
$.when.apply($, deferreds).then(mapData(data));
}
function addLocation(job) {
var dfd = $.Deferred(),
url = url;
console.log('in the addLocation function, outside the ajax call');
$.ajax({
url: url,
dataType: 'jsonp',
jsonp: 'json_callback'
}).done(function(location) {
job.lnglat = [parseFloat(location[0].lon), parseFloat(location[0].lat)];
console.log('resolved location');
dfd.resolve();
});
return dfd.promise();
}
function mapData(data) {
console.log('in the mapData function');
console.log(data);
var point = svg.selectAll('points')
.data(data);
point.exit().remove();
point.enter().append('circle')
.attr('r', 2.5);
point
.attr('cx', function(d) {
console.log(d); //shows d and has the lnglat but I think that is due to the developer tools reading the final result, not the result at the time
console.log(d.lnglat); // this gives undefined
console.log(d.lnglat[0]); // this gives an error as it hasn't been defined
console.log(projection(d.lnglat[0]));
return projection(d.lnglat[0])[0];
})
.attr('cy', function(d) {
return projection(d.lnglat[1])[1];
});
}
Chrome 开发者工具报告以下顺序:
in the addLocation function, outside the ajax call //x10
[Object, object...] //this is the object list of deferred objects, there are 10 of them
in the mapData function
[Object, object..] //this is the object list of data from the mapData function
Object > ... //this is the d object in the mapData function > point anonymous function
undefined // this is the result of d.lnglat
Uncaught typeError ... // this is the result of d.lnglat[0] which doesn't exist yet
resolvedLocation //x10 this should be a lot higher right?
所以我希望 resolvedLocation console.log
在 mapData 函数
运行之前运行,我以为我已经这样设置了,但显然不工作。我错过了什么?
最佳答案
正如adeneo在评论中所说,这里的问题是在
中对mapData的立即调用$.when.apply($, deferreds).then(mapData(data))
要解决这个问题,您需要使用绑定(bind)来保持大部分解决方案完好无损(并且不要对其进行太多更改)。这应该有效:
$.when.apply($, deferreds).then(mapData.bind(this, data))
绑定(bind)不会延迟函数的调用。它将数据参数绑定(bind)到函数的调用,因此当“then”函数调用mapData时,它会使用“data”作为其参数,并且只有在所有 promise 都得到解决之后才会发生。
关于javascript - Deferred、Promise 和 Ajax 的顺序不正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28003664/
在 javascript 中使用 defer 属性的正确语法是什么? 我见过有两种方式: 1: ... 2: ... 根据经验 [和我找不到的引用资料],我更倾向于使用第二个选项,但我只是仔细检查了
有什么区别 var dfd = new $.Deferred 和 var dfd = $.Deferred 在哪些情况下需要使用 new 与不使用它? 最佳答案 jQuery official doc
给定使用 Promise 构造函数的代码 let promise = () => new Promise(resolve => resolve(1)); new Promise((resolve, r
我正在研究 RxKotlin,问题出现了:defer() 和 defer{} 有什么区别 最佳答案 defer() 和 defer {} 只是写同一件事的两种方式。 Kotlin 在某些特定情况下允许
我正在尝试使用 Python 在 Google App Engine 上执行此操作: def add_to_db(person): a = PersonDb(key_name = perso
所以我有一个延迟对象数组dataCalls。 目前我正在使用以下方法来解雇所有这些,并在所有 Deferreds 解决后调用回调: $.when.apply(null, dataCalls) .
考虑以下几点: function foo(){ // Returns the jQuery deffered below. var urlToUse; $.ajax({
我刚刚看到此代码已被弃用,我一直在尝试遵循此指南:http://www.codelord.net/2015/09/24/$q-dot-defer-youre-doing-it-wrong/以正确的方式
我有一些元素,其中一些代码依赖于其他 中的代码元素。我看到了defer属性在这里可以派上用场,因为它允许推迟执行代码块。 为了测试它,我在 Chrome 上执行了这个:http://jsfiddle
我需要向 jQuery 的 .when() 监视的函数添加未知数量(仅在运行时已知)的 .pipe() 调用。这些调用将基于另一个异步操作的 ajax 调用。请参阅下面的代码以获得更清晰的解释: $.
我正在使用 GAE 的“deffered” ' 库 (python),它会在出现异常时自动重试任务。 有没有办法知道(在任务处理函数中)任务已尝试的次数? 我的最终目标是实现如下内容: if num_
我有几个 元素,其中一些代码依赖于其他 中的代码元素。我看到了defer属性在这里可以派上用场,因为它允许代码块在执行中被推迟。 为了测试它,我在 Chrome 上执行了这个:http://jsfi
在我看来,两者都做同样的事情。 文档: deferred.always() deferred.then() 最佳答案 看起来deferred.then()允许您传递两个单独的成功和失败回调,而 def
我正在使用 Twisted 编写代码,但在为我的扭曲互联网延迟变量想出一个合理的变量名时遇到了麻烦。这是我的候选人: d :太通用,太短,违反了 pylint 规则 C0103。 def :与内置函数
是否存在这样的情况:调用 .callback() 或 .errback() 会向调用者引发异常,而该异常不会被延迟捕获? 假设我有以下延迟和回调: from twisted.internet impo
这是代码: http://jsbin.com/lizami/edit?js,console 将代码也粘贴到此处: var aaa = $.Deferred(); var bbb = function(
这是我的尝试: deferred.defer(class1().method1, class2.method2, arg) deferred.defer(class1().method1, class
我正在尝试使用 google.appengine.ext.deferred 来运行任务。我正在将一个方法传递给 defer() 方法,该方法成功运行,但在返回时抛出一个 ValueError: F
这个问题在这里已经有了答案: What's the difference between a Deferred object and its own promise object? (3 个答案)
我下载了一个名为 jsdeferred 的库来尝试帮助我解决一些代码流问题,但我有点迷茫,因为它的示例和...“文档” 在某些事情上有点不清楚。但是当我继续阅读和挖掘,当然还有谷歌搜索阳光下的一切时,
我是一名优秀的程序员,十分优秀!