Skip to content Skip to sidebar Skip to footer

Create A Spinner For My Html Page

I have a web form which the user needs to submit to the server. The server response may take seconds. So I want to show the user a spinner, such that all other fields are grayed an

Solution 1:

Here's my complete solution, using ajax to send the query (you can't have a spinner if you leave the page by using a standard form post) :

loading = {
    count: 0
};

loading.finish = function() {
    this.count--;
    if (this.count==0) this.$div.hide();
};

// 
loading.start = function() {
    this.count++;
    if (!this.$div) {
        var html = '<div style="position: fixed;z-index:100;left:0;top:0;right:0;bottom:0;background: black;opacity: 0.6;">';
        html += '<table width=100% height=100%>';
        html += '<tr><td align=center valign=middle>';
        html += '<img src=img/loading.gif>';
        html += '</td></tr>';
        html += '</table></div>';
        this.$div=$(html);
        this.$div.prependTo('body');
    }
    setTimeout(function(){
        if (loading.count>0) loading.$div.show();
    }, 500);
};

// the function to call
askUrl = function(url, success) {
    var httpRequest = newXMLHttpRequest();
    httpRequest.onreadystatechange = function() {
        if (httpRequest.readyState === 4) {
            if (httpRequest.status === 200) {
                success(msg);
            }
            loading.finish();
        }
    };
    loading.start();
    httpRequest.open('GET', url);
    httpRequest.send();
};

If you call askUrl and the servers doesn't answer in 500 ms, the screen is greyed and a spinner is displayed.

I got my gif spinner here.

Post a Comment for "Create A Spinner For My Html Page"