0

I created a function, which gets executed every 500 ms. On localhost it works fine, but if I upload it to a webhosting site (hostpoint), the function stops get called after some time (around 1 minute). Does a JavaScript call limit exists?

Here is the code:

abc();

function abc() {
    console.log('request');
    setTimeout(function () {
        abc();
    }, 500);
}
c3560231
  • 9
  • 5
  • 1
    There is no call limit for functions. You could overflow the stack with a recursive function but `setTimeout` is safe from that. There is a different problem. – VLAZ Oct 11 '19 at 11:00
  • how do you know it stopped? share the link of the page – Dhananjai Pai Oct 11 '19 at 11:00
  • 1
    It depends entirely on what the `abc()` function does. If it's making an AJAX request for example, then it's an incredibly bad idea. Also note that you can just use `setInterval()` instead of a recursive call – Rory McCrossan Oct 11 '19 at 11:01
  • @RoryMcCrossan the abc() function does an AJAX request. – c3560231 Oct 11 '19 at 11:03
  • working fine for me, maybe your console was full and it started to overwrite, which browser ? – Dhananjai Pai Oct 11 '19 at 11:03
  • @DhananjaiPai time didn't change too (um XX:XX aktualisiert). – c3560231 Oct 11 '19 at 11:04
  • 1
    `the abc() function does an AJAX request` then that's the problem. You're DDOSing your own server as you're flooding it with requests. It's likely your hosting company is detecting this and preventing the attack. If you need to keep the server and client in close sync then use websockets or server sent events. AJAX polling is not a workable solution - as you've found. – Rory McCrossan Oct 11 '19 at 11:06
  • @RoryMcCrossan Can you explain me how server sent events work? I've created a new question here: https://stackoverflow.com/questions/58341878/how-to-create-basic-server-sent-events – c3560231 Oct 11 '19 at 13:52
  • I'm not familiar with PHP, but [this question](https://stackoverflow.com/questions/14512182/how-to-create-websockets-server-in-php) seems to cover how to create a WebSocket server using it. Also note that WS is a completely different method of communication to HTTP. You will most likely need to restructure your application to use them effectively. – Rory McCrossan Oct 11 '19 at 13:56

0 Answers0