Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

334
Views
How to update timestamp in local storage?

I need to store and update a timestamp in local storage so after one minute the modal will execute each time. I want the modal to appear on refresh only if the time stamp has expired or gone past one minute. One minute duration is just for testing purposes. Goal is to only show a modal once a day to user. Here is what I have so far. It only shows modal on refresh and stores local storage key. But after one minute the modal does not appear on refresh and should.

    $(document).ready(function() {
          function shouldShowModal() {
              const lastTimestamp = localStorage.getItem("modalTimestamp");
              if (!lastTimestamp) {
                  return true;
              }
              const parsedTimestamp = parseInt(lastTimestamp, 10);
              let expiryLimit = new Date(parsedTimestamp);
              expiryLimit = expiryLimit.setMinutes(expiryLimit.getMinutes() + 1);
              return new Date() > expiryLimit;
          }
    
          if (window.localStorage) {
              const currentUser = '<%= current_user %>';
              const currentTime = new Date($.now());
              if (currentUser && shouldShowModal()) {
                  swal ({
                      icon: 'warning',
                      text: 'Notice text here.',
                      closeOnClickOutside: false,
                      button: 'ok'
                  })
                  localStorage.setItem('modalTimestamp', currentTime);
              }
          }
    }

Updated working with @Barmar solution:

    $(document).ready(function() {
          function shouldShowModal() {
              const lastTimestamp = localStorage.getItem("modalTimestamp");
              if (!lastTimestamp) {
                  return true;
              }
              const parsedTimestamp = parseInt(lastTimestamp, 10);
              let expiryLimit = new Date(parsedTimestamp);
              expiryLimit = expiryLimit.setHours(expiryLimit.getHours() + 24);
              return new Date() > expiryLimit;
          }
    
          if (window.localStorage) {
              const currentUser = '<%= current_user %>';
              const currentTime = new Date();
              if (currentUser && shouldShowModal()) {
                  swal ({
                      icon: 'warning',
                      text: 'Notice text here.',
                      closeOnClickOutside: false,
                      button: 'ok'
                  })
                  localStorage.setItem('modalTimestamp', currentTime.getTime());
              }
          }
    }
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

currentTime is a Date object, you need to save the numeric timestamp in local storage. So use

localStorage.setItem('modalTimestamp', currentTime.getTime());

I also don't see any need to use $.now() when setting currentTime. new Date() defaults to returning the current time.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!