How To Store Static Value From Variable That Keep Changes In Javascript
I have created a function that gives current UTC date and time: get_current_UTCDate: function() { var d = new Date(); return d.getUTCFullYear() +'-'+ (d.get
Solution 1:
This code demonstrates the basic functionality you need (I only used part of your code)...
<html><head><scriptsrc="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script><scripttype="text/javascript">
obj = {
get_current_UTCDate: function() {
var d = newDate();
return d.getUTCFullYear() +"-"+ (d.getUTCMonth()+1) +"-"+d.getUTCDate()+" "+ d.getUTCHours()+":"+ d.getUTCMinutes()+":"+ d.getUTCSeconds();
},
on_timer: function() {
if (localStorage.getItem("curdate") == null) {
localStorage.setItem("curdate",obj.get_current_UTCDate());
alert('first time');
}
alert(localStorage.getItem("curdate"));
}
}
$(document).ready(function(){
$('button').on("click", obj.on_timer);
});
</script></head><body><button>click</button></body></html>
Solution 2:
Without knowing all of the requirements I would look into using a cookie to store the value. Since you are already using jQuery you could use the $.cookie plugin, or just use a basic set/get routine like:
functioncookies() {
returndocument.cookie.split('; ').reduce(function(acc, v) {
p = v.split('='); acc[p[0]] = p[1]; return acc;
}, {});
}
functiongetCookie(key) {
returncookies()[key];
}
functionsetCookie(key, value) {
returndocument.cookie = [key, '=', value, ';'].join('');
}
Then in your code something like:
if ($(e.target).hasClass("pt_timer_start")) {
if (saved_date = getCookie('current_date')) {
current_date = saved_date;
} else {
current_date = this.get_current_UTCDate();
setCookie('current_date', current_date);
}
this.set_current_timer_activity({date: current_date});
this.start_interval();
}
Post a Comment for "How To Store Static Value From Variable That Keep Changes In Javascript"