0

我有一个包含任务的 html 表,最后一列称为“状态”。我需要做一件简单的事情,用动画 gif 图标的常用方式跟踪每个任务的状态,如果它正在进行中,或者检查它是否完成。

我过去通过使用 $.ajax 调用 php 脚本来做到这一点,它每 1 分钟刷新一次。该脚本是一个 PHP 脚本,其中包含一个检查布尔列的查询。这种方法还可以,但即便如此,我也知道这很可能是做这种事情的最愚蠢的方法。

我正在寻找一种更强大的方法来做到这一点,而我在搜索时发现了这个片段

    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>

<script type="text/javascript">
$(document).ready(function() {
$.ajaxSetup({ cache: false }); // This part addresses an IE bug.  without it, IE will only load the first number and will never refresh
setInterval(function() {
$('#divToRefresh').load('/path/to/your/php/file/userCount.php');
}, 3000); // the "3000" here refers to the time to refresh the div.  it is in milliseconds.
});
</script>

像这样的东西可以工作,但如果任务实际完成,我该如何停止刷新?像“Gaurav Sharma”这样的帖子回答是要走的路吗?

我是 Jquery 的新手,所以我真的很想知道你会如何做这样的事情,这似乎是一项常见的任务,所以我犹豫是否通过编写 2 页代码来重新发明轮子来完成可以用 5 行完成的事情: -)

4

2 回答 2

0
var interval = setInterval(function() {
    $('#divToRefresh').load('/path/to/your/php/file/userCount.php');
    if(somecondition){
        clearInterval(interval);
    }
}, 3000); // the "3000" here refers to the time to refresh the div.  it is in milliseconds.

调用clearInterval(interval)停止重复。

于 2012-10-27T10:53:19.467 回答
0

你应该实现一些这样的代码

var interval = null; // has to be global in this example

// [...] your code

interval = setInterval(function(...));

// [...] your code

如果您的脚本完成只需clearInterval(interval)从任何地方调用

于 2012-10-27T10:54:50.040 回答