satinez satinez - 6 months ago 21
Javascript Question

Run JS SetTimeout function only once per domain

I've implemented a Splash Screen in JQuery on my WordPress Site on Document ready.

setTimeout(function() {
$("#site-overlay").fadeOut();
}, 3000);


Ok, the Overlay fades out after 3 seconds. But if I click on a menu button, the whole page reloads and the function
settimeout
repeats because its on the document ready.

How can I set this only for one time per Domain without using complicated code?

Answer

What you will have to do is implement cookies or something alike

I would suggest https://github.com/yanivkalfa/myCookie which is my own library but there are other like its so you can pick one.

The point is to set a flag or something in a cookie or session that says that you already done the action you want.

e.g:

  // <script .... https://github.com/yanivkalfa/myCookie .. inclode my cookies library

  setTimeout(function() {
    var overlayFlag = $.fn.myCookie({cName: "overlayFlag"});
    if (!overlayFlag) {
      $.fn.myCookie( {cName : "overlayFlag", cVal : true} );
      $("#site-overlay").fadeOut();
    }
  }, 3000);

Something along these lines, there are other solutions .. this seemed a simple.

Comments