locked
timer in every view RRS feed

  • Question

  • User-819032351 posted

    hi

    I want to timer in every view for example 00:19:59 and Subtract one unit from it every second 00:19:59 ...00 : 18 : 32 ... 

    And if the user enters another page, this timer will not start from the beginning

    for example in viewA : 00:18 : 23

    and 3 seconds in viewB show : 00 : 18 : 19

    tnx

    Monday, August 3, 2020 2:42 PM

All replies

  • User-474980206 posted

    this is done with javascript, browser session storage and and timer:

    @if (firstPage) {
    <script>
       // render on frst page
       sessionStorage.setItem("timer", @maxSeconds);
    </script>
    }
    
    <script>
       // get saved time 
       var remainingSeconds = sessionStorage.getItem("timer") || 0;
       var timer = setInterval(function() {
           --remainingSeconds;
           if (remainingSeconds < 0)
           {
               // stop timer
               clearInterval(timer);
               // do end of timer stuff
               alert("times up");
           }
       },1000);
    </script>

    note: if you must support older browsers, you should use a hidden field  included with the post data

    Monday, August 3, 2020 3:03 PM
  • User475983607 posted

    Create a JS file that contains the logic and include the file in every page.  Use standard state management to persist the time left.  This can be a cookie or local storage. 

    Keep in mind that this problem has been solved and a basic Google should find a solution.

    https://www.google.com/search?q=javascript+countdown+timer+seconds

    Monday, August 3, 2020 3:05 PM