如果 - 否则 - Localstorage不工作/ cordova

时间:2015-06-26 21:44:45

标签: javascript cordova if-statement

我想检查它的第一个应用是否开始。我通过usb上的iphone上的xcode运行我的应用程序。但每次我关闭iphone上的应用程序并通过单击手机上的图标再次启动它 - 就像重新启动一样 - 它不会识别该应用程序之前是startet。我做错了什么?

$(document).ready( function() {
    if (localStorage.getItem("applaunch")) {
        window.localStorage.getItem("callstatus");

    }else{
        //Local storage is not set, hence first time launch. set the local storage item
        window.localStorage.setItem('applaunch',1);
        window.localStorage.setItem("vipstatus", "0");
        window.localStorage.setItem("callstatus", "0");
    }

    function generateUUID() {
        var d = new Date().getTime();
        var uuid = 'xxxxxxxxxxxxxxxyxxxxxxxxxxxxxxx'.replace(/[xy]/g,function(c) {
            var r = (d + Math.random()*16)%16 | 0;
            d = Math.floor(d/16);
            return (c=='x' ? r : (r&0x7|0x8)).toString(16);
        });
        return uuid.toUpperCase();
    }

    window.localStorage.setItem("username",generateUUID());

    var username = window.localStorage.getItem("username");
    var vipstatus = window.localStorage.getItem("vipstatus");
    var callstatus = window.localStorage.getItem("callstatus");

    $.ajax({
        type: 'post',
        url: 'http://www.bla.com/action.php',
        data: {
            data: {"username": username, "vipstatus" : vipstatus, "callstatus" : callstatus},
        },
        success: function(result) {
            console.log(result);
        }
    });
});

每次我重新启动手机上的应用程序(不再通过XCODE BUILDING),它在我的数据库中创建一个新条目 - 请参阅ajax。

1 个答案:

答案 0 :(得分:1)

设置一个变量,说明它是否是第一次。然后只在变量为true时才进行AJAX调用。

$(document).ready( function() {
    var first_time;
    if (localStorage.getItem("applaunch")) {
        first_time = false;
    }else{
        //Local storage is not set, hence first time launch. set the local storage item
        window.localStorage.setItem('applaunch',1);
        window.localStorage.setItem("vipstatus", "0");
        window.localStorage.setItem("callstatus", "0");
        first_time = true;
    }

    function generateUUID() {
        var d = new Date().getTime();
        var uuid = 'xxxxxxxxxxxxxxxyxxxxxxxxxxxxxxx'.replace(/[xy]/g,function(c) {
            var r = (d + Math.random()*16)%16 | 0;
            d = Math.floor(d/16);
            return (c=='x' ? r : (r&0x7|0x8)).toString(16);
        });
        return uuid.toUpperCase();
    }

    if (first_time) {    
        window.localStorage.setItem("username",generateUUID());
    }

    var username = window.localStorage.getItem("username");
    var vipstatus = window.localStorage.getItem("vipstatus");
    var callstatus = window.localStorage.getItem("callstatus");

    if (first_time) {
        $.ajax({
            type: 'post',
            url: 'http://www.bla.com/action.php',
            data: {
                data: {"username": username, "vipstatus" : vipstatus, "callstatus" : callstatus},
            },
            success: function(result) {
                console.log(result);
            }
        });
    }
});