Time Spent on Website

    0

    0

    Felix Kaboto

    This code is an example of using an interval timer. The interval timer is defined as an object called fxTimer . The interval timer lasts for 1000 milliseconds and it is set to run once every time the sessionStorage.getItem('fxtimer') function is called. If the value of the fxTimer object is 'sent', then the interval timer is disabled. If the value of the fxTimer object is not 'sent', then the interval timer is enabled and it will run every 1000 milliseconds.

    <script>
      var fxIntTimer = setInterval(function(){
      var fxTimer = sessionStorage.getItem('fxtimer'),
        dataLayer = window.dataLayer || [];
      if(fxTimer && fxTimer !== 'sent'){
        if(parseFloat(fxTimer) >= 6000){
        console.log('event, disable timer');
        dataLayer.push({ event: 'fx-timer' });
        sessionStorage.setItem('fxtimer', 'sent');
        clearInterval(fxIntTimer);
        } else {
        console.log('exists plus 1');
        sessionStorage.setItem('fxtimer', parseFloat(fxTimer) + 1000);
        }
      } else if(fxTimer !== 'sent') {
        console.log('plus 1');
        sessionStorage.setItem('fxtimer', 1000);	
      }
      }, 1000);
      </script>
    Codiga Logo
    Codiga Hub
    • Rulesets
    • Playground
    • Snippets
    • Cookbooks
    soc-2 icon

    We are SOC-2 Compliance Certified

    G2 high performer medal

    Codiga – All rights reserved 2022.