Web Connection
Refresh web page with AJAX every X seconds up to Y times
Hello,
A web page lists report requests that the user has made and their status.
- Reports that are ready to view are marked "Listo!"
- Reports that will be ready to view in very few seconds are marked as "Waiting!"
- Reports that require human intervention are marked as "Pendiente!"
If there are any reports that are "Waiting!" we want the page to refresh using AJAX, but want to limit the number of times it refreshes in case trouble rears it's ugly head.
We now use the following code:
<script type="text/javascript">
$(document).ready(function() {
var table_ReportList = $('#Table_ReportList').DataTable( {
"ajax": "JSONListar_Solicitudes.pp?UserPk=<%=Session.UserPk%>",
"columns": [
{ "data": "ctitulo2" },
{ "data": "tcreada" },
{ "data": "solid" },
{ "data": "cstatus"},
{ "data": "chtmlstatus" }
],
"order": [[2, 'desc']],
language: {
url: 'localisation/spanish.txt'
},
"columnDefs": [
{"className": "dt-center", "targets": "_all"}]
});
setInterval(function () {
table_ReportList.ajax.reload(null, false); // user paging is not reset on reload
},5000);
} )
</script>
Thanks in advance,
Alex
setInterval()
returns a token that you can use with clearInterval()
to cancel a timer loop. Store that token in a variable and count up the number of times the counter fires and clear the interval when you get over your treshold. Or if there's some cancel command coming back from the server or the request is done you do the same.
+++ Rick ---