Reputation: 21
I installed Apache in my Raspberry Pi3 to get a local website.
I have this JavaScript in my index.php
that I need to refresh every 200ms :
function Function()
{
$.ajax({
url : 'variable.php',
data: {action: 'variable'},
type: 'post',
success: function(output){
document.getElementById("demo").innerHTML= output;
setInterval(Function(),200);
}
});
}
In variable.php
, it's just an output of a command.
But, in 2 minutes, the free memory goes from 220 000 KiB to 40 000 KiB so my website begin to freeze in 10 minutes and I must close my web internet and reopen it.
What's wrong ?
Upvotes: 2
Views: 1709
Reputation: 490213
It becomes more or less an endless recursion problem because in every callback for the XHR you are calling Function
(because you have parenthesis there, making an invocation and not a reference) as well as setting it to be called every 200ms. You should only pass the reference to the function there, and use setTimeout()
instead.
setTimeout(Function, 200);
Also, Function
is a bad name for a function. It will also clobber the Function
constructor.
Upvotes: 6
Reputation: 1351
It is a recursive call which should be handled wisely. I recommend you call recursive function on Ajax.Success callback that will help you recall the function if the previous Ajax call succeeded. And you will prevent throttling your server.
Upvotes: 1