Javascript 在 AJAX 错误时继续
我对重新加载网页的可靠性有一个小问题。我有这个 php 页面需要每 15-20 秒运行一次。我的主机不允许使用 Web cron 或 perl 脚本,因此我的解决方案是让页面每 15-20 秒重新加载一次。我的主机有点不可靠,每隔 8-10 小时我就会收到一个 404 错误,这会中断操作。
然后,我用 HTML 和 Javascript 做了一个小控制部分:
<html>
<body>
<script type="text/javascript">
setInterval(var returner = new XMLHttpRequest();returner.open("GET","processIncoming.php",false);returner.send("");', 15000);
</script>
</body>
</html>
这并不比我的上一个解决方案好,如果我的主机返回错误,Internet Explorer 将给出“指定资源下载失败”的错误,然后停止脚本执行不再有。有没有办法告诉它在出现任何错误时继续,或者有一个更好的解决方案,不涉及使用我的主机不允许的权限,例如 cron、web cron、perl、exec()、set_time_out( )?
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()?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用 try catch 语句:
You could use a try catch statement:
只需使用 jQuery:
不要忘记插入 jQuery 库本身。
Just use jQuery:
Don't forget to insert the jQuery library itself.