dcsimg
www.webdeveloper.com
Page 2 of 2 FirstFirst 12
Results 16 to 18 of 18

Thread: 24hours auto purge code

  1. #16
    Join Date
    Apr 2017
    Posts
    10
    Thanks to you guys.

    Now the problem is that this timer is working but anytime I refresh it will start afresh. Kindly help look into it.

    Code:
     <script>
            function startTimer(duration, display) {
                var start = Date.now(),
                diff,
                hours,
                minutes,
                seconds;
                function timer() {
                    // get the number of seconds that have elapsed since
                    // startTimer() was called
                    diff = duration - (((Date.now() - start) / 1000) | 0);
    
                    // does the same job as parseInt truncates the float
                    hours = (diff / 3600) | 0;
                    
               minutes = ((diff / 60) % 60) | 0;
                    seconds = (diff % 60) | 0;
    
                    hours = hours < 10 ? "0" + hours : hours;
                    minutes = minutes < 10 ? "0" + minutes : minutes;
                    seconds = seconds < 10 ? "0" + seconds : seconds;
    
                    display.textContent = hours + ":" + minutes + ":" + seconds;
    
                    if (diff <= 0) {
                        clearInterval(countdownTimer);
                    }
                };
                // we don't want to wait a full second before the timer starts
                timer();
                countdownTimer = setInterval(timer, 1000);
            }
    
            window.onload = function () {
                var twoMinutes = 60,
                    twentyfourHours = 60 * 60 * 24,
                display = document.querySelector('#time');
                startTimer(twentyfourHours , display);
            };
        </script>
        <!-- language: lang-html -->
        <!-- end snippet -->
        <span id="time" style="background:white;border:2px solid green;color:green;paddding:5px;"></span> To Delete Your Account

  2. #17
    Join Date
    Dec 2012
    Posts
    1,495
    This saves the starting time in local storage and load it at page load:
    Code:
        <script>
            function startTimer(duration, display) {
                var startingtime = localStorage.getItem("startingtime");
                if (!startingtime) var startingtime = Date.now();
                var diff,
                minutes,
                seconds;
                window.addEventListener("beforeunload", function () {
                    if (diff > 0) localStorage.setItem("startingtime", startingtime);
                });
                function timer() {
                    // get the number of seconds that have elapsed since
                    // startTimer() was called
                    diff = duration - (((Date.now() - startingtime) / 1000) | 0);
    
                    // does the same job as parseInt truncates the float
                    hours = (diff / 3600) | 0;
                    minutes = ((diff / 60) % 60) | 0;
                    seconds = (diff % 60) | 0;
    
                    var additionalText = " ";
                    if (diff < 60 * 60) {
                        additionalText += minutes + " minutes left";
                    } else {
                        additionalText += hours + " hours and a few minutes left";
                    }
    
                    hours = hours < 10 ? "0" + hours : hours;
                    minutes = minutes < 10 ? "0" + minutes : minutes;
                    seconds = seconds < 10 ? "0" + seconds : seconds;
    
                    display.textContent = hours + ":" + minutes + ":" + seconds + additionalText;
    
                    if (diff <= 0) {
                        localStorage.removeItem("startingtime");
                        if (countdownTimer) clearInterval(countdownTimer);
                    }
                };
                // we don't want to wait a full second before the timer starts
                timer();
                var countdownTimer = setInterval(timer, 1000);
            }
    
            window.onload = function () {
                var twoMinutes = 60 * 2,
                    twentyfourHours = 60 * 60 * 24,
                display = document.querySelector('#time');
                startTimer(twoMinutes, display);
            };
        </script>
        <!-- language: lang-html -->
        <!-- end snippet -->
        <span id="time" style="background:white;border:2px solid green;color:green;paddding:5px;"></span> To Delete Your Account

  3. #18
    Join Date
    Apr 2017
    Posts
    10
    Quote Originally Posted by Sempervivum View Post
    This saves the starting time in local storage and load it at page load:
    Code:
        <script>
            function startTimer(duration, display) {
                var startingtime = localStorage.getItem("startingtime");
                if (!startingtime) var startingtime = Date.now();
                var diff,
                minutes,
                seconds;
                window.addEventListener("beforeunload", function () {
                    if (diff > 0) localStorage.setItem("startingtime", startingtime);
                });
                function timer() {
                    // get the number of seconds that have elapsed since
                    // startTimer() was called
                    diff = duration - (((Date.now() - startingtime) / 1000) | 0);
    
                    // does the same job as parseInt truncates the float
                    hours = (diff / 3600) | 0;
                    minutes = ((diff / 60) % 60) | 0;
                    seconds = (diff % 60) | 0;
    
                    var additionalText = " ";
                    if (diff < 60 * 60) {
                        additionalText += minutes + " minutes left";
                    } else {
                        additionalText += hours + " hours and a few minutes left";
                    }
    
                    hours = hours < 10 ? "0" + hours : hours;
                    minutes = minutes < 10 ? "0" + minutes : minutes;
                    seconds = seconds < 10 ? "0" + seconds : seconds;
    
                    display.textContent = hours + ":" + minutes + ":" + seconds + additionalText;
    
                    if (diff <= 0) {
                        localStorage.removeItem("startingtime");
                        if (countdownTimer) clearInterval(countdownTimer);
                    }
                };
                // we don't want to wait a full second before the timer starts
                timer();
                var countdownTimer = setInterval(timer, 1000);
            }
    
            window.onload = function () {
                var twoMinutes = 60 * 2,
                    twentyfourHours = 60 * 60 * 24,
                display = document.querySelector('#time');
                startTimer(twoMinutes, display);
            };
        </script>
        <!-- language: lang-html -->
        <!-- end snippet -->
        <span id="time" style="background:white;border:2px solid green;color:green;paddding:5px;"></span> To Delete Your Account
    Thank you I will use it now

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles