HTML, CSS, JavaScript
How to change a bit of javascript using AJAX
Gravatar is a globally recognized avatar based on your email address. How to change a bit of javascript using AJAX
  Alejandro A Sosa
  All
  Feb 27, 2017 @ 02:31pm

I'm developing a server that receives end user requests for reports. These reports are not created in the server itself because the data is distributed in several hundred places. The server sends the request to the appropriate site to prepare the report (using SignalR thanks to Rick).

As soon as the server receives the request and forwards it to be processed, it returns to the user a list of all requests that s/he has made. If a request hasn't received an answer yet, the list shows a waiting message and gif. If a request already has an answer available it shows buttons to View, Download or Forget the report.

See attachments.

Right now the page has javascript that includes the following, which causes the page to be refreshed every 5 seconds

        setInterval(function () {
            table_ReportList.ajax.reload(null, false);  // user paging is not reset on reload
        }, 5000);

I would like that bit of javascript to change the interval every time it fires, depending if some reports are waiting or all are ready to view.

Any suggestions are appreciated. TIA,

Alex

Gravatar is a globally recognized avatar based on your email address. re: How to change a bit of javascript using AJAX
  Rick Strahl
  Alejandro A Sosa
  Feb 28, 2017 @ 01:08pm

Not sure I understand fully what you're asking, but if you want to change the timeout to setInterval() you can do that by using a outer scope variable variable and capturing the interval object. You can release the interval and create a new one with the new timeout.

Something like this:

// this has to be outer scope so it stays alive
var interval = null;

function createInterval(timeout) {
    interval = setInterval(function() {
        // do something on an interval
    
        if(intervalNeedsToChange) {
            // create new timeout that's one second longer than previous
            var newTimeout = timeout + 1000; 
            
            // reset the interval  
            clearInterval(interval);
            createInterval(newInterval, newTimeout);
        }
    },timeout);
}

+++ Rick ---

Gravatar is a globally recognized avatar based on your email address. re: How to change a bit of javascript using AJAX
  Alejandro A Sosa
  Rick Strahl
  Mar 3, 2017 @ 07:28am

I'll try along those lines. Thanks

© 1996-2024