简体   繁体   中英

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.getUTCMonth()+1) +"-"+d.getUTCDate()+" "+_.str.sprintf("%02d", d.getUTCHours())+":"+_.str.sprintf("%02d", d.getUTCMinutes())+":"+_.str.sprintf("%02d", d.getUTCSeconds());

that has been called into another function :

on_timer: function(e) {
            var self = this;
            if ($(e.target).hasClass("pt_timer_start")) {
                current_date = this.get_current_UTCDate();    
                this.set_current_timer_activity({date: current_date});
                this.start_interval();
                }

And this on_timer function is called into a toggle button.

this.$el.find(".pt_timer_button button").on("click", this.on_timer);

The problem:

On each time when i press start button it takes new value from current_date . And my condition is, if button is pressed for 1st time than take FRESH value from current_date, And if page is refreshed and button is again pressed then it should take that FIRST value. (It should not take another fresh value).

So is there any way to store first value of current_date into some another variable X, and let it keep static. Or may I use cookies?

Thanks in advance .

This code demonstrates the basic functionality you need (I only used part of your code)...

<html>
  <head>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
    <script type="text/javascript">

    obj = {
      get_current_UTCDate: function() {
        var d = new Date();
        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>  

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:

function cookies() {
    return document.cookie.split('; ').reduce(function(acc, v) {
        p = v.split('='); acc[p[0]] = p[1]; return acc;
    }, {});
}

function getCookie(key) {
    return cookies()[key];
}

function setCookie(key, value) {
    return document.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();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM