简体   繁体   English

JQuery getJSON导致无限循环

[英]JQuery getJSON causing infinite loop

I can't seem to figure out why this block of code causes an infinite loop. 我似乎无法弄清楚为什么这段代码会导致无限循环。 I suspect that by update the html elements I am causing the load event to be re-triggered and therefore getJSON is called repeatedly. 我怀疑通过更新html元素,我导致重新触发load事件,因此重复调用getJSON。 If that's the case, any suggestions on how to call getJSON when the page (or a specific element loads) without causing the loop? 如果是这种情况,有关如何在页面(或特定元素加载)时调用getJSON而不引起循环的任何建议?

$("#content").on("load",function(){
$.getJSON(url, function(data) {
$('#div1').html(data.Name);
});
});

If you are reloading the content element from $.getJSON , then it will go to infinite loop. 如果要从$.getJSON重新加载content元素,那么它将进入无限循环。 I don't know what type is your content element. 我不知道你的内容元素是什么类型。 But I think , dont call it onload . 但我认为,不要把它称为onload Trigger the JSON call on change of something that you are not going to change inside your $.getJSON 触发JSON调用以更改您在$.getJSON中不会更改的$.getJSON

You can try to load this when the DOM is ready (ie the page is rendered completely) 您可以尝试在DOM准备就绪时加载它(即页面完全呈现)

Try this 尝试这个

$(document).ready(function(){
   $.getJSON(url, function(data) {
     $('#div1').html(data.Name);
   });
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM