jQuery - 轮询作业队列

发布于 2024-08-20 04:39:43 字数 403 浏览 6 评论 0原文

我有一个包含作业队列的数据库表。一个单独的程序处理这些作业。我想提供一个网页供用户观看队列的进度。用于查询表并以 JSON 格式返回的服务器端脚本没有问题。

我读过一些关于 jQuery 和 PeriodicalUpdater 插件 的文章。我想知道是否有可能使用这个插件来创建一个可视队列(基本解释是一个单列表,队列中每个条目一行),其中已完成的作业将在下一个队列中删除投票时间。正如我所说,服务器端脚本不是问题,我只是无法理解这种 UI/动画。任何进一步阅读的提示将不胜感激,或者如果我完全找错了树,也请告诉我。

提前致谢

I have a database table which contains a queue of jobs. A separate program processes these jobs. I want to provide a webpage for users to watch the progress of the queue. The Server side scripting to query the table and return it in a JSON format is no problem.

I've done some reading on jQuery and the PeriodicalUpdater plugin. I'm wondering if it is at all possible to use this plugin to create a visual queue (a basic explanation would be a one column table, with a row per entry in the queue), where jobs which have been completed are removed the next time it polls. As I say, the server side script isn't a problem, I just can't get my head around this sort of UI/Animation. Any tips for further reading would be appreciated, or if I'm barkin up the wrong tree entirely please let me know also.

Thanks in advance

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

像你 2024-08-27 04:39:43

我不熟悉 periodicalupdater 插件,但如果我是你,我会考虑简单地使用 JavaScript 的“setTimeout”函数。 setTimeout 允许您以指定的时间间隔运行函数。

有关与您的情况相关的示例,请参见此处:

<script type="text/javascript">

setTimeout("updateTable();", 5000);

function updateTable()
{
     $.post("/your_script.php", {}, function(result) {

          $("#my_table").html(result);

     });
     setTimeout("updateTable", 5000);
}

</script>

注意:一秒有 1000 毫秒,因此该函数设计为每 5 秒触发一次。

另外...

我假设队列表中的每个条目都有一个与其关联的唯一 ID。在您的 HTML 主页面中,我将打印出如下所示的表格:

<table>
<tr id='q1'><td>Queue Item 1</td></tr>
<tr id='q2'><td>Queue Item 2</td></tr>
<tr id='q3'><td>Queue Item 3</td></tr>
<tr id='q4'><td>Queue Item 4</td></tr>
<tr id='q5'><td>Queue Item 5</td></tr>
</table>

换句话说,为队列表中的每一行分配与表中条目相同的 ID。然后,当您的 AJAX 调用返回结果时,您可以检查是否有任何队列项目已完成。如果有,您可以执行以下操作:

$("#q1").fadeOut("fast");

I'm not familiar with the PeriodicalUpdater plugin, but if I was you, I would look into simply using JavaScript's "setTimeout" function. setTimeout allows you to run a function at a specified interval.

For an example that's relevant to your situation, see here:

<script type="text/javascript">

setTimeout("updateTable();", 5000);

function updateTable()
{
     $.post("/your_script.php", {}, function(result) {

          $("#my_table").html(result);

     });
     setTimeout("updateTable", 5000);
}

</script>

Note: There are 1000 milliseconds in one second, so that function is designed to fire off every 5 seconds.

In addition...

I'm assuming that each entry in your queue table has a unique ID associated with it. Within your main HTML page, I would print out the table like this:

<table>
<tr id='q1'><td>Queue Item 1</td></tr>
<tr id='q2'><td>Queue Item 2</td></tr>
<tr id='q3'><td>Queue Item 3</td></tr>
<tr id='q4'><td>Queue Item 4</td></tr>
<tr id='q5'><td>Queue Item 5</td></tr>
</table>

In other words, assign each row in your queue table the same ID that the entry in your table has. Then, when your AJAX call returns a result, you can check to see if any of the queue items have been finished. If they have, you can do something like:

$("#q1").fadeOut("fast");
我爱人 2024-08-27 04:39:43

您不需要长时间轮询。您只需要定期使用 ajax 调用来使用 setInterval 刷新队列。每次调用服务器时,您都会获取整个现有队列,与当前队列进行比较,并删除不再存在的队列:

<script type="text/javascript">
var old_result = {};
setInterval(function(){
     $.post(YOUR_URL, {}, function(result) {
       //compare result with old_result
       //remove missing elements using an animation on that row
       //set old_result to result
     });
}), 5000);

</script>

You don't need long polling. You just need a periodic ajax call to refresh the queue using setInterval. Every time you call the server, you get the entire existing queue, compare to the current queue, and remove the ones no longer present:

<script type="text/javascript">
var old_result = {};
setInterval(function(){
     $.post(YOUR_URL, {}, function(result) {
       //compare result with old_result
       //remove missing elements using an animation on that row
       //set old_result to result
     });
}), 5000);

</script>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文