Malasorte Malasorte - 2 months ago 8
Javascript Question

Each time alert is shown increment sessionStorage value

$.jAlert({
'content' : 'Now I will show this alert and increment sessionStorage my_variable by 1.',
'onClose' : function(alert) {
sessionStorage.setItem("my_variable", "this_is_the_value_i_need_to_increment");
}
});


The first time this alert is shown "this_is_the_value_i_need_to_increment" should be 1. The second time the alert is shown "this_is_the_value_i_need_to_increment" should be 2. And so on.

Is this possible? Thank yoy!

Answer

just parse it to integer and increment it by 1 and set item again as follows:

$.jAlert({
    'content' : 'Now I will show this alert and increment sessionStorage my_variable by 1.',
    'onClose' : function (alert) {
        sessionStorage.setItem("my_variable", 1 + parseInt(sessionStorage.getItem("my_variable")))
    }
});

and of course, my_variable should be initialized before the alert like:

sessionStorage.setItem("my_variable",0);