-2

in my javascript I defined a poller in order to refresh some contents every second. The definition is the following:

var poller = {
    // number of failed requests
    failed: 0,

    // starting interval - 1 seconds
    interval: 1000,

    // kicks off the setTimeout
    init: function() {
        setTimeout(
            $.proxy(this.getData, this), // ensures 'this' is the poller obj inside getData, not the window object
            this.interval
        );
    },

    // get AJAX data + respond to it
    getData: function() {
        var self = this;

        $.ajax({
            url: "api/view",
            success: function(response) {

                   // ....do something....

                    // recurse on success
                    self.init();
                }
            },

            error: $.proxy(self.errorHandler, self)
        });
    },

    // handle errors
    errorHandler: function() {
        if (++this.failed < 10) {

            this.interval += 1000;

            // recurse
            this.init();
        }
      }
    };

    poller.init();
});

The problem is that it does not start immediately when the page is loaded. Does anyone know the reason why? Many thanks in advance!

ctt_it
  • 339
  • 1
  • 6
  • 22
  • 1
    *The problem is that it does not start immediately when the page is loaded* - When **does** it start ? – Koby Douek May 04 '17 at 13:05
  • Works just fine: https://jsfiddle.net/q4rvzv0o/ (if I remove the trailing `});`). Naturally, the first time it does `getData` is 1000ms after `init` runs, since that's what you've told it to do. – T.J. Crowder May 04 '17 at 13:06
  • You need to clarify what "it does not start immediately when the page is loaded." means. What do you expect to happen, what actually happens, how do they differ, etc. – pvg May 04 '17 at 13:06
  • 1
    Your code suggests that no matter where you call the `init()` method, it will wait 1000 milliseconds before it does anything. This is indicated by the `setTimeout()` call in `init()`. – kevin628 May 04 '17 at 13:06
  • I expect that after 1000 ms the function is called. Instead the first call is done after 40 seconds more or less. – ctt_it May 04 '17 at 13:10
  • 1
    Have you examined this behaviour in your browser's dev console ? – Koby Douek May 04 '17 at 13:11
  • @T.J.Crowder yes, the '});' are just the final parenthesis of the script – ctt_it May 04 '17 at 13:11
  • 1
    This is obviously happening because of your `this.interval += 1000;` statement. You need to figure out why is it reaching this code line. – Koby Douek May 04 '17 at 13:11
  • @KobyDouek I add a breakpoint at that line, but it is never reached – ctt_it May 04 '17 at 13:16
  • Are you using `$(document).ready()` around all of this declaration? – Koby Douek May 04 '17 at 13:17
  • yes. `$(document).ready(function() {` . This is the reason why for the `});` – ctt_it May 04 '17 at 13:20
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/143403/discussion-between-koby-douek-and-ctt-it). – Koby Douek May 04 '17 at 13:23

1 Answers1

1

This is happening because of the response time from the 1st $.ajax call. When calling a service in localhost (dev environment), calls may sometimes take more than 40 seconds.

Also, please see if any other element, such as Highcharts you are using in your specific page, cause any delay on loading the page.

You can examine this behavior in your browser's developer console, under Network. Here is an example of the response time of one of my $.ajax calls (I'm using Google Chrome):

enter image description here

I also suspect that when you move this to production, your response times will be much lower, but you will have to take into consideration that 1000 milliseconds might be too aggressive on your server.

Koby Douek
  • 16,156
  • 19
  • 74
  • 103
  • The problem wasn't on the poller but on what was done in the function. The function defined an HighChart chart and this operation is very slow when I have many points. Once I made it faster, everything works as expected. – ctt_it May 04 '17 at 15:51
  • @ctt_it Updated the answer accordingly. Accepting it as correct would be highly appreciated. – Koby Douek May 04 '17 at 16:20