每隔15秒发送一个又一个AJAX ASYNC请求
我想以 15 秒的间隔从 WebServer(PHP) 轮询数据 50 到 100 次(或者说无限循环,直到 stopFlag 变量设置为 true。)。 对于此数据轮询,我将使用 AJAX ASYNC 消息将请求发送到 Web 服务器。 我怎样才能做到这一点? 我尝试自己解决这个难题,但不幸的是,我失败了,因为javascript中没有用于暂停脚本执行的关键字。 有什么办法让它发挥作用吗?或对此有任何解决方法吗?如果您已经遇到此问题,请告诉我或分享您的经验。
I want to poll the data from the WebServer(PHP) at an interval of 15 seconds, for 50 to 100 times (or lets say infinite loop, until the stopFlag variable is set to true.).
For this data polling, i am going to use the AJAX ASYNC message for sending the requests to the WebServer.
How can i achieve this?
I have tried to solve this puzzle by myself, but unfortunatly, i failed as there is no keyword for pausing the script execution in the javascript.
Is there any way to make it work? or any workaround on this? Kindly let me know or share your experience if you have already faced this issue.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您必须使用超时回调;它将递归调用下一个函数。
您还可以使用 jQuery,它可以帮助您使代码更加紧凑。结果可能如下所示:
在 params 中,您将拥有如下所示的成功函数:
只需第一次调用 keepTrying() 即可;它将循环直到成功。这段代码有点难看,但希望你能明白。
You have to use a callback for the timeout; it will recursively call the next function.
You can also use jQuery which can help you to make your code more compact. The result might look something like this:
And in params you would have a success function like this:
Just call keepTrying() the first time; it will loop until it is successful. This code is a bit ugly but hopefully you get the idea.