aboutsummaryrefslogtreecommitdiffstats
path: root/offline-submit/john-resig-offline-app-stub.js
blob: b9d616045dd0cedfe87e72a39205937bba6d74dd (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
// stub for the offline application in pseudocode
// from http://ejohn.org/blog/offline-events/

function saveData( item ){
    if ( navigator.onLine ) {
        saveToServer( item );
    } else {
        toSave.push( item );
    }
}

function loadData( item ){
    if ( navigator.onLine ) {
        return loadFromServer( item );
    } else {
        var result = loadFromQueue( item );
        if ( !result ) {
            displayError();
            toLoad.push( item );
        }
        return result;
    }
}

setInterval(function(){
    if ( navigator.onLine ) {
        var item = predictNextItemToBeLoaded();
        loadData( item );
    }
}, 5000);

window.ononline = function(){
    toSave.forEach( saveData );
    toLoad.forEach( loadData );
};

window.onload = function(){
    document.getElementById("myform").onsubmit = function(){
        saveData( this );
        return false;
    };
};