Web Connection
Refresh web page with AJAX every X seconds up to Y times
Gravatar is a globally recognized avatar based on your email address. Refresh web page with AJAX every X seconds up to Y times
  Alejandro A Sosa
  All
  May 7, 2020 @ 11:41am

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

Gravatar is a globally recognized avatar based on your email address. re: Refresh web page with AJAX every X seconds up to Y times
  Rick Strahl
  Alejandro A Sosa
  May 7, 2020 @ 12:03pm

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

Gravatar is a globally recognized avatar based on your email address. re: Refresh web page with AJAX every X seconds up to Y times
  Alejandro A Sosa
  Rick Strahl
  May 7, 2020 @ 12:22pm

Thank you! Appreciate it.

Alex

© 1996-2024