Ajax调用返回任务状态? (进行中或已完成) [英] Ajax call to return a task status? ( in progress OR finished)

查看:189
本文介绍了Ajax调用返回任务状态? (进行中或已完成)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含任务的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行:-)

解决方案

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)停止重复.

I have a html table with tasks and the last column is called "status". I need to do a simple thing, to track the status of each task with the usual way of an animating gif icon if its in progress or an ok check if its done.

I have done this in the past by calling a php script with $.ajax that it was refreshing itself every 1 minute. The script was a PHP script with one query that checked a boolean column. This approach was ok, but even then I knew that this was most probably the dumbest way to do something like this.

I am looking for a more robust way to do this and while I was searching I found this snippet

    <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>

Something like this could work but how do I stop this refresh if the task was actually finished? Something like "Gaurav Sharma" answer on this post is the way to go?

I am a newbie in Jquery so I would really love to know how would you do something like this, seems a common task so I hesitate to re-invent the wheel by writing 2 pages of code for something that can be done with 5 lines :-)

解决方案

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.

calling clearInterval(interval) stops the repetition.

这篇关于Ajax调用返回任务状态? (进行中或已完成)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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