1

I want to add a class after some time. but the delay method is not working in sessionStorage. I don't know why it's not working.

window.onload = function() {
  if (sessionStorage.getItem(".bts-popup") != 'true') {
    $('body.home, body.page-template-page-services').delay(10000).addClass('is-visible');
    sessionStorage.setItem('.bts-popup', 'true');
  }
}
Alessio Cantarella
  • 5,077
  • 3
  • 27
  • 34
Anandhan
  • 11
  • 1
  • Not working as it is not running? Or what? Not working can mean a lot of things. Is there an error? – Max Baldwin Sep 09 '19 at 14:00
  • Possible duplicate of [What is the JavaScript version of sleep()?](https://stackoverflow.com/questions/951021/what-is-the-javascript-version-of-sleep) – freedomn-m Sep 09 '19 at 14:01
  • 1
    delay is for things that get queued up like animations. https://api.jquery.com/delay/ – epascarello Sep 09 '19 at 14:02
  • FYI: Hiding the `` tag when the page firsts loads for 10 seconds would be subject to a Google SEO penalty. – Reactgular Sep 09 '19 at 14:04

1 Answers1

5

This has nothing to do with session storage, it is entirely due to the method you're calling after delay; addClass is not added to any queue.

The docs hint at this:

The .delay() method is best for delaying between queued jQuery effects. Because it is limited—it doesn't, for example, offer a way to cancel the delay—.delay() is not a replacement for JavaScript's native setTimeout function, which may be more appropriate for certain use cases.

In your case simply use

setTimeout(function(){
    $('body.home, body.page-template-page-services').addClass('is-visible');
},10000);
Jamiec
  • 133,658
  • 13
  • 134
  • 193