Poll the Server with Ajax and Dojo

孤街醉人 提交于 2019-12-07 12:04:54

问题


I'm using dojo.xhrPost to sent Ajax Requests
The call is wrapped by a function sendRequest()

I've now to continuously (every 3sec) send the same ajax Post to the server
How can I implement a Server Poll with Dojo? I basically need to call sendRequest() every 3 secs


回答1:


I don't believe that Dojo has a method built-in for polling, so here's a generic method that's applicable across frameworks

var Poll = function(pollFunction, intervalTime) {
    var intervalId = null;

    this.start = function(newPollFunction, newIntervalTime) {
        pollFunction = newPollFunction || pollFunction;
        intervalTime = newIntervalTime || intervalTime;

        if ( intervalId ) {
            this.stop();
        }

        intervalId = setInterval(pollFunction, intervalTime);
    };

    this.stop = function() {
        clearInterval(intervalId);
    };
};

Usage:

var p = new Poll(function() { console.log("hi!"); }, 1000);
p.start();
setTimeout(function() { p.stop();}, 5000);

Or in your case:

var p = new Poll(sendRequest, 3000);
p.start();

If you want this as a Dojo package, then the following is a trivial extension:

dojo.provide("Poll");

dojo.declare("Poll", null, {
    intervalId:   null,
    pollFunction: null,
    intervalTime: null,

    constructor: function(newPollFunction, newIntervalTime) {
        this.pollFunction = newPollFunction;
        this.intervalTime = newIntervalTime;
    },

    start: function(newPollFunction, newIntervalTime) {
        this.pollFunction = newPollFunction || this.pollFunction;
        this.intervalTime = newIntervalTime || this.intervalTime;

        this.stop();
        this.intervalId = setInterval(this.pollFunction, this.intervalTime);
    },

    stop: function() {
        clearInterval(this.intervalId);
    }
});

Usage:

var p = new Poll(function() {console.log("hi");}, 250);
p.start();
setTimeout(dojo.hitch(p, p.stop), 1000);



回答2:


I have found it better to do like this:

  1. Have a variable that contains an empty array (queue)
  2. setInterval to poll, on each poll, push a new object (with the poll parameters) into the array (queue); you may also compress polls by collapsing objects with the same parameters into one single object; you may even put handler functions into these objects
  3. Have a timer to inspect the queue; if nothing, returns
  4. If there is a pending object in the queue, checks if there is already a pending xhr operation that has not returned, just wait -- you don't want too many xhr's pending simultaneously, some devices (e.g. iPad) chokes on it
  5. If there is no pending xhr operation, dequeue the first poll object and xhrGet it

The benefit of this procedure is that you can easily throttle poll intervals, works fine when certain xhr operations time out, and can easily implement privatization of polling requests.




回答3:


To continuously update your grid you can include your ajax request in the 'refresh-complete' callback function of the grid.

yourGrid.on('dgrid-refresh-complete', function(event) { 

//Ajax request fireing every 3 sec


}


来源:https://stackoverflow.com/questions/2455900/poll-the-server-with-ajax-and-dojo

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