jQuery - polling of a job queue

喜你入骨 提交于 2019-12-04 17:19:34

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");

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>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!