如果-否则-本地存储不起作用/codova

If - else - Localstorage dont work / cordova

本文关键字:不起作用 codova 存储 否则 -本 如果      更新时间:2023-09-26

我想检查它是否是第一次启动应用程序。我在usb上通过iphone上的xcode运行我的应用程序。但每次我关闭iphone上的应用程序,然后点击手机上的图标重新启动它——就像重新启动一样——它都不会意识到这个应用程序以前是启动的。我做错了什么?

$(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。

设置一个变量,说明是否是第一次。然后只在变量为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);
            }
        });
    }
});