Whendy
Whendy

Reputation: 11

Stop or Crash my browser when i load data from database

Hy guys I have the following code in my JS file

 function load(){
   $.ajax({
         url: "http://blah-blah.com/file.php",
         type: "POST",
         data: {data:"blah"},
         typeData: "json",
         success: function(data){
          $("#divLoad").html(data);
          load();
         }     
   });
var reload = setInterval({"load()"}, 5000);
// or can use this
var reload = setTimeout({"load()"}, 5000);
}

and the following in file.php

 point to --> echo json_encode('<div id="a">$r[name]</div><div id="b">$r[sex]</div>');

Why does executing the above code hang my browser / computer, and shows a very high CPU usage?

Upvotes: 1

Views: 171

Answers (1)

Steve
Steve

Reputation: 20469

Because load is a recursive function, it calls itself in the success callback.

By calling it periodically with setInterval, you will progressively build up more and more ajax calls.

Remove the recursive load() call in the success function and you are good to go:

function load(){
   $.ajax({
         url: "http://blah-blah.com/file.php",
         type: "POST",
         data: {data:"blah"},
         typeData: "json",
         success: function(data){
          $("#divLoad").html(data);
          //load(); <--remove this
         }     
   });
}

Upvotes: 2

Related Questions