I am using the PhoneGap LocalNotification plugin, which allows me to set local notifications at certain times.
The basic structure of the LocalNotification plugin is like this:
var notification = {
init: function () {
},
clear_all: function () {
notification.clear();
plugins.localNotification.add({
badge: 0,
});
},
alert_ten: function () {
var d = new Date();
d = d.getTime() + 10 * 1000; //60 seconds from now
d = new Date(d);
plugins.localNotification.add({
date: d,
repeat: 'daily',
message: varone + ' - ' + vartwo + '!',
hasAction: true,
badge: 1,
id: '1',
sound: 'horn.caf',
background: 'app.background',
foreground: 'app.running'
});
},
}
If you look at the message part of the notification, it consists of the following varone + ' - ' + vartwo + '!'. On page load, varone and vartwo are populated from a localStorage item. I then call notification.alert_ten() onLoad.
This all works fine, but there is one exception:
The localStorage items are set when the user interacts with a certain div, i.e., clicks it. Then, when the app is loaded, it checks to see these values, and alerts the message after 10 seconds, saying that this and that, which get their values from the LS.
If the user changes their mind, and interacts with a different div, which changes the LS items, the LocalNotification still runs with the original set of LS items.
This is to be expected, as JS will cache the variable within the function. A solution that I thought would work would be to define the variables globally, above var notification = {, and then when the user interacts with the div, update the vars to represent the new variables.
Global variables:
var varone = localStorage.getItem("favorite");
var vartwo = localStorage.getItem("favorite-status").substr(2).toLowerCase();
...
Updated variables:
...
var varone = text;
var vartwo = favstatus;
...
The function, notification.alert_ten() still runs with the original values defined within the global variable, instead of the updated ones.
_naming convention. Would be more javascript syntax to name the functionsclearAll()andalertTen().