-1

I'm trying to clear some of the local storage items after 2 minutes using this code

I'm saving current time in the variable like :

   let time_now  = (new Date()).getTime();
   localStorage.setItem('otp_save_time', time_now);
   localStorage.setItem('set_otp', value);

Now i'm checking the current time with the saved time and clearing the item if time is greater than 2 minutes, but it is not clearing the item.

    let time_now  = (new Date()).getTime();
    if((time_now - saved_time) > 2 * 60 * 1000) {
       localStorage.removeItem('set_otp');
       localStorage.removeItem('otp_save_time');
    } 
user3653474
  • 3,393
  • 6
  • 49
  • 135

1 Answers1

-1

would you like to use

setTimeout(function(){localStorage.removeItem('your key');}, 120 * 1000);

it more simple

garudamon
  • 197
  • 5