score:-1

If you use jquery, then you can send an AJAX request using the $.ajax function. Make sure you handle the response in the result's done() function, as success is deprecated.

Plain AJAX request example:

HTML:

 <!DOCTYPE html>
<html>
<body>

<div id="demo"><h2>Let AJAX change this text</h2></div>

<button type="button" onclick="loadDoc()">Change Content</button>

</body>
</html> 

JS:

function loadDoc() {
  var xhttp = new XMLHttpRequest();
  xhttp.onreadystatechange = function() {
    if (this.readyState == 4 && this.status == 200) {
     document.getElementById("demo").innerHTML = this.responseText;
    }
  };
  xhttp.open("GET", "ajax_info.txt", true);
  xhttp.send();
} 

Taken from here. If you mastered AJAX requests, then the next step is to write a poller, using setInterval. The first parameter should be a function which sends a request and the second should be the time between two execution in milliseconds (10000 in this case). Or you can use an existing poller. This is one I have implemented:

function Initializable(params) {
    this.initialize = function(key, def, private) {
        if (def !== undefined) {
            (!!private ? params : this)[key] = (params[key] !== undefined) ? params[key] : def;
        }
    };
}

function Poller(params) {
    Initializable.call(this, params);

    var that = this;
    this.initialize("url", window.location.href);
    this.initialize("interval", 5000);
    this.initialize("type", "POST");
    this.initialize("method", "POST");
    this.initialize("data", {});
    this.initialize("strict", true);

    var defaultFunction = function() {};

    this.initialize("done", defaultFunction);
    this.initialize("fail", defaultFunction);
    this.initialize("always", defaultFunction);

    this.isRunning = function() {
        return !!params.intervalID;
    };

    this.run = function() {
        if (this.strict && (this.green === false)) {
            return;
        }
        this.green = false;
        $.ajax({
            url: this.url,
            method: this.method,
            data: this.data
        }).done(function(data, textStatus, jqXHR) {
            that.green = true;
            that.done(data, textStatus, jqXHR);
        }).fail(function(jqXHR, textStatus, errorThrown) {
            that.green = true;
            that.fail(jqXHR, textStatus, errorThrown);
        }).always(function(param1, param2, param3) {
            that.green = true;
            that.always(param1, param2, param3);
        });
    };

    this.start = function() {
        if (!params.intervalID) {
            this.run();
            params.intervalID = setInterval(this.run.bind(this), this.interval);
        }
    };

    this.stop = function() {
        if (!!params.intervalID) {
            clearInterval(params.intervalID);
            params.intervalID = undefined;
        }
    };

}

Related Query

More Query from same tag