- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
array1
是 ["orange","blue"]
。
$.getJSON("path1.json",function(array1){
for (var i = array1.length - 1; i >= 0; i--) {
var path2 = array1[i];
console.log(path2);
$.getJSON(path2,function(someObject){
console.log("Inside the second $.getJSON function");
console.log(path2);
});
}
});
输出如下所示。
"orange"
"blue"
"Inside the second $.getJSON function"
"blue"
"Inside the second $.getJSON function"
"blue"
为什么输出不是这样的?
"orange"
"Inside the second $.getJSON function"
"orange"
"blue"
"Inside the second $.getJSON function"
"blue"
最佳答案
发生了两件事:
$.getJSON()
是部分异步的。这意味着您的回调是异步发生的。var
声明的变量的作用域为函数,而不是 block ,并且,虽然您可以使用 var
在给定作用域中重新声明变量,但这样做没有效果。当您组合这些东西时,您最终会遇到这样一种情况:for
循环的所有迭代都在调用任何回调之前完成,因此,当回调发生时,path2
已更新多次。 (巧合的是,这实际上不会影响内部 $.getJSON()
调用本身,因为 path2
是按值传递的。)
在过去,我们必须修复 path2
值的范围(通常通过 IIFE ),以便在执行回调之前它不会被覆盖:
$.getJSON("path1.json", function(array1){
for (var i = array1.length - 1; i >= 0; i--) {
var path2 = array1[i];
console.log(path2);
$.getJSON(path2,
function(path2) {
return function(someObject){
console.log("Inside the second $.getJSON function");
console.log(path2);
};
}(path2)
);
}
});
现在,我们有了 let
,它将变量的范围限定在 block 内。 for
的 block 作用域在每次迭代时都会创建新的,并且每次创建回调函数时该作用域实例都会绑定(bind)到回调,因此以下工作有效:
$.getJSON("path1.json",function(array1){
for (var i = array1.length - 1; i >= 0; i--) {
let path2 = array1[i];
console.log(path2);
$.getJSON(path2, function(someObject){
console.log("Inside the second $.getJSON function");
console.log(path2);
});
}
});
关于javascript - 内部 $.getJSON 不会转到循环中的第二个元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56916939/
我正在尝试获取客户端的 IP 地址和 GPS 坐标。使用 jQuery,我有这个: $.getJSON("http://jsonip.appspot.com/", function(data) {
这个问题已经有答案了: Why does JQuery.getJSON() have a success and a done function? (2 个回答) 已关闭 6 年前。 我想知道这两个代
所以首先,是的,我知道有几个答案,但没有一个能够解决我的问题。首先我将展示我的代码HTML: Shop
假设我有以下 JavaScript: (function($) { $.getJSON(url, function(data) { $.each(data.rows, function(i
是否可以使用 jQuery 在另一个 getJSON 请求中使用 getJSON 请求? 像这样: // Population the Requests List // jQuery AJAX cal
我有以下代码,该代码已针对此问题进行了简化。基本上我有一个循环,在每次迭代中调用 jquery getJSON 函数,调用 API 端点来获取一些天气数据。问题是,当 getJSON 请求被触发时,我
我有一个使用 getJSON 的函数,但它没有像我预期的那样工作。 function balbla(name, param) { $.getJSON("/blabla.json?nam
我有一段代码,例如: $.getJSON("http://mysite.org/polls/saveLanguageTest?url=" + escape(window.location.href)
我正在使用jquery.getJSON() ,但我不知道如何进行错误处理。这些是我需要处理的一些情况。 1)如果返回的数据为null怎么办? 2)如果返回的数据不能解析json怎么办? 3) 如果返回
我正在通过参与一个测试项目(包括 SubSonic 和 jQuery)来学习 asp.net mvc。 我遇到的问题是,每次我想要返回的不仅仅是简单字符串(例如 Json 对象)时,我都会遇到困难,因
执行跨域查询,如果运行的URL不可用(404),如何执行某个功能?我尝试这样的事情: $.getJSON({ url:'example.php?callback=?', statusCode: { 4
我在 jQuery 中搜索了相关主题,但没有找到任何方法来解决我的问题。 $(document).ready(function(){ $("#inputForm").submit(functi
当调用 yahoo Web 服务 (http://boss.yahooapis.com/ysearch) 返回数据集时,是否可以设置超时并在超时后退出例程? jQuery.getJSON("http:
我正在使用 jQuery getJSON() 函数。这个函数获取数据没有问题。但有时等待,等待等待......我的加载栏在页面中心显示加载加载。 所以 jQuery ajax() 函数有一个超时变量。
我有一个 html 代码: asd $('button').click( function() { $.getJSON('/schedule/test/', function
目标:我所追求的是每次在数据库中添加某些内容时(在 $.ajax 到 Submit_to_db.php 之后),从数据库获取数据并刷新 main.php(通过 draw_polygon 更明显)。 所
我已经阅读了文档并用谷歌搜索了此内容,但没有看到问题所在。我正在尝试从本地 json 文件获取一些数据。我已在 JSONLint 验证了响应数据 代码 $(document).ready(functi
我在json.getJSON方法上遇到麻烦。这是我当前的代码: var jqxhr = $.getJSON("http://127.0.0.1:5002?callback=?", function()
我需要进行跨域请求,并且 getJSON 有问题。 $.getJSON("http://usr:pwd@10.0.1.xx/cgi-bin/remote/request.cgi?m=json&r=gr
这个问题已经有答案了: How do I return the response from an asynchronous call? (42 个回答) 已关闭 8 年前。 这是我的问题 我有一个充满
我是一名优秀的程序员,十分优秀!