取消Javascript超时 [英] Cancel Javascript timeout

查看:198
本文介绍了取消Javascript超时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Web服务器上托管了一个很长的进程。只需用户点击按钮,即可从网页触发该事物。一些Javascript定期通过Ajax进行轮询,以检查操作是否已完成服务器端。为此,我使用 setInterval ,稍后使用 clearInterval 来停止轮询。

I have a long process hosted on a Web Server. The thing is triggered from a Web Page, at the click of a button by a user. Some Javascript polls regularly via Ajax to check if the operation has completed server side. To do this, I use setInterval, and later on clearInterval to stop polling.

如果这需要太长时间(例如服务器已经崩溃),我希望通过某种超时来通知客户端。我做了一些研究,发现了 setTimeout 。问题是,如果操作在超时之前成功完成,我想取消这个。

If this takes too long (e.g. server has crashed), I'd like the client to be informed by some sort of timeout. I've done some research and found about setTimeout. Problem is, if the operation finishes successfully before the timeout, I'd like to cancel this one.


  1. 怎么做?

  2. 你会建议采用不同的方法吗?

PS:我特别针对 IE7 / IE8 ,但总是对某些 JQuery 开放

PS : I'm targetting IE7/IE8 in particular, but always open to some JQuery

推荐答案

只要将间隔的id存储在变量中,就可以随时使用它来清除间隔。 / p>

As long as you store your interval's id in a variable, you can use it to clear the interval at any time.

var interval = window.setInterval(yourFunction, 10000);

...elsewhere...

window.clearTimeout(interval);

有关详细信息,请参阅 Mozilla Documentation的setInterval示例

For more information see the Mozilla Documentation's setInterval example.

组合一个包含 Mozilla示例的修改版

这篇关于取消Javascript超时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆