0

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. If that's the case, any suggestions on how to call getJSON when the page (or a specific element loads) without causing the loop?

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

2 Answers 2

1

You can try to load this when the DOM is ready (i.e. the page is rendered completely)

Try this

$(document).ready(function(){
   $.getJSON(url, function(data) {
     $('#div1').html(data.Name);
   });
});
Sign up to request clarification or add additional context in comments.

2 Comments

I tried using $(document).ready however the result is the same. After the div1 element is changed the ready event fires again which puts it in an infinite loop.
Disregard my last comment. I found the problem, I was experimenting with setInterval and it seems that was causing loop issue.
1

If you are reloading the content element from $.getJSON , then it will go to infinite loop. I don't know what type is your content element. But I think , dont call it onload. Trigger the JSON call on change of something that you are not going to change inside your $.getJSON

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.