Search code examples
javascriptajaxreload

Javascript Continue On AJAX Error


I have a slight problem with the reliability of reloading web pages. I have this php page that needs to be ran every 15-20 seconds. No web cron or perl script is allowed by my host, so my solution is to have the page reload itself every 15-20 seconds. Slight problem of my host being slightly unreliable, and I'll get a 404 error every 8-10 hours, and that breaks the operation.

Then, I made a little controlling piece in HTML and Javascript:

<html> 
<body>
<script type="text/javascript">
setInterval(var returner = new XMLHttpRequest();returner.open("GET","processIncoming.php",false);returner.send("");', 15000);
</script>
</body>
</html> 

This is no better than my last solution, if my host returns an error, Internet Explorer will give an error of "Download of specified resource failed" and then stops the script from executing any more. Is there any way to tell it to just continue if there are any errors, or an even better solution that doesn't involve using privileges that my host won't allow such as cron, web cron, perl, exec(), set_time_out()?


Solution

  • You could use a try catch statement:

    try
      {
      //Run some code here
      }
    catch(err)
      {
      //Handle errors here
      }