dcsimg
www.webdeveloper.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 18

Thread: 24hours auto purge code

  1. #1
    Join Date
    Apr 2017
    Posts
    10

    24hours auto purge code

    Please I need someone to please help me re fix this code to 24hours auto purge instead of this 1hr

    <!-- begin snippet: js hide: false -->

    <!-- language: lang-js -->
    <script>
    function startTimer(duration, display) {
    var start = Date.now(),
    diff,
    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
    minutes = (diff / 60 * 60) | 0;
    seconds = (diff % 60) | 0;

    minutes = minutes < 10 ? "0" + minutes : minutes;
    seconds = seconds < 10 ? "0" + seconds : seconds;

    display.textContent = minutes + ":" + seconds;

    if (diff <= 0) {
    // add one second so that the count down starts at the full duration
    // example 05:00 not 04:59
    start = Date.now() + 1000;
    }
    };
    // we don't want to wait a full second before the timer starts
    timer();
    setInterval(timer, 1000);
    }

    window.onload = function () {
    var fiveMinutes = 60 * 2,
    display = document.querySelector('#time');
    startTimer(fiveMinutes, 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.

  2. #2
    Join Date
    Mar 2007
    Location
    localhost
    Posts
    4,503
    That is just over blown code.

    You have a serious flaw in the timer() function...
    Code:
    // we don't want to wait a full second before the timer starts
    timer();
    setInterval(timer, 1000);
    }
    Completely the wrong way to use setInterval.
    You use it like this
    Code:
    }
    // put setInterval outside of the function and give it a variable so you can clearInterval at a later date if needed.
    timerHandler = setInterval( timer, 1000);
    so it is outside of the function, not in it and calling the fimer function without "" or () will call it immediately.

    This is an example of a simple countdown clock, it counts down to midnight of the current day
    Code:
    <div id="midnightcountdown"><script>
    	d = new Date();
    	midnightHour = new Date(d.getFullYear(),d.getMonth()+1,d.getDate(),23,59,59); // todays date
    	midnight = {
    		target:false,
    		end:midnightHour.getTime(),
    		tick:function(){
    			this.now = new Date();
    			if( (this.now.getTime() < this.end ) ){
    				this.remain = new Date( this.end - this.now.getTime() ).toUTCString().slice(16,25);
    				}else this.remain = "00:00:00"; //<-- this is where your purge widget would go to trigger the purge.
    			if( this.target ) this.target.innerHTML = "Left : " + this.remain;
    		},
    		init:function(){
    			if( !this.target ) this.target = document.getElementById("midnightcountdown");
    			if( this.target ){
    				clearInterval(this.auto);
    				this.auto = setInterval("midnight.tick()",500);
    				
    			}  
    		},
    		auto:setInterval("midnight.init()",999)
    	}
    </script></div>
    What you don't need to do is track or calculate hours, minutes, do any padding by using the UTC string method, you can chop out the time, get a date reference that can be used in a date object and as shown, get a countdown by using the date object to calculate the remaining time and grab a ready formatted string.

    This timer is based on the user local machine, if you want a countdown based on the server and its location, then you need to use a bit of PHP to facilitate grabbing this information and setting the timer to display time left on the server irrespective of timezone. As you have not said, its assumed that the clients timezone is the relevant one.
    --> JavaScript Frameworks like JQuery, Angular, Node <--
    ... and please remember to wrap code with forum BBCode tags:-

    [CODE]...[/CODE] [HTML]...[/HTML] [PHP]...[/PHP]

    If you can't think outside the box, you will be trapped forever with no escape...

  3. #3
    Join Date
    Apr 2017
    Posts
    10
    I will test the given one and get back to you on any conditions gotten from it.

    Thank you for your time.

  4. #4
    Join Date
    Apr 2017
    Posts
    10
    When I use the code you sent earlier it was still the same thing. I'm thinking of you can please help me look into this and compare to the first code I sent.

    Thanks in anticipation


    Code:
     <h2>MERGED DETAILS!</h2></div></div>
    <div class="fa-boardx" style="width:90%;border:2px solid grey;"></div>
    <h5>You Are making payment to:</h5>';
    if($set['autopurge'] > 0){
    	$MyTime = ceil(($mergto['xtime'] - time())/1);
    	if($MyTime >= '1440' && $MyTime > '1380'){
    		$TimeR = '24 Hours Few Minutes';
    	}
    	elseif($MyTime >= '1380' && $MyTime > '1320' && $MyTime < '1440'){
    		$TimeR = '23 Hours Few Minutes';
    	}
    	elseif($MyTime >= '1320' && $MyTime > '1260' && $MyTime < '1380'){
    		$TimeR = '22 Hours Few Minutes';
    	}
    	elseif($MyTime >= '1260' && $MyTime > '1200' && $MyTime < '1320'){
    		$TimeR = '21 Hours Few Minutes';
            }
    	elseif($MyTime >= '1200' && $MyTime > '1140' && $MyTime < '1260'){
    		$TimeR = '20 Hours Few Minutes';
    	}
    	elseif($MyTime >= '1140' && $MyTime > '1080' && $MyTime < '1200'){
    		$TimeR = '19 Hours Few Minutes';
    	}
    	elseif($MyTime >= '1080' && $MyTime > '1020' && $MyTime < '1140'){
    		$TimeR = '18 Hours Few Minutes';
    	}
    	elseif($MyTime >= '1020' && $MyTime > '960' && $MyTime < '1080'){
    		$TimeR = '17 Hours Few Minutes';
    	}
    	elseif($MyTime >= '960' && $MyTime > '900' && $MyTime < '1020'){
    		$TimeR = '16 Hours Few Minutes';
    	}
    	elseif($MyTime >= '900' && $MyTime > '840' && $MyTime < '960'){
    		$TimeR = '15 Hours Few Minutes';
            }
    	elseif($MyTime >= '840' && $MyTime > '780' && $MyTime < '900'){
    		$TimeR = '14 Hours Few Minutes';
    	}
    	elseif($MyTime >= '780' && $MyTime > '720' && $MyTime < '840'){
    		$TimeR = '13 Hours Few Minutes';
    	}
    	elseif($MyTime >= '720' && $MyTime > '660' && $MyTime < '780'){
    		$TimeR = '12 Hours Few Minutes';
    	}
    	elseif($MyTime >= '660' && $MyTime > '600' && $MyTime < '720'){
    		$TimeR = '11 Hours Few Minutes';
    	}
    	elseif($MyTime >= '600' && $MyTime > '540' && $MyTime < '660'){
    		$TimeR = '10 Hours Few Minutes';
    	}
    	elseif($MyTime >= '540' && $MyTime > '480' && $MyTime < '600'){
    		$TimeR = '9 Hours Few Minutes';
    	}
    	elseif($MyTime >= '480' && $MyTime > '420' && $MyTime < '540'){
    		$TimeR = '8 Hours Few Minutes';
    	}
    	elseif($MyTime >= '420' && $MyTime > '360' && $MyTime < '480'){
    		$TimeR = '7 Hours Few Minutes';
    	}
    	elseif($MyTime >= '360' && $MyTime > '300' && $MyTime < '420'){
    		$TimeR = '6 Hours Few Minutes';
    	}
    	elseif($MyTime >= '300' && $MyTime > '240' && $MyTime < '360'){
    		$TimeR = '5 Hours Few Minutes';
    	}
    	elseif($MyTime >= '240' && $MyTime > '180' && $MyTime < '300'){
    		$TimeR = '4 Hours Few Minutes';
    	}
    	elseif($MyTime >= '180' && $MyTime > '120' && $MyTime < '240'){
    		$TimeR = '3 Hours Few Minutes';
    	}
    	elseif($MyTime >= '120' && $MyTime > '60' && $MyTime < '180'){
    		$TimeR = '2 Hours Few Minutes';
    	}
    	elseif($MyTime >= '60' && $MyTime < '120'){
    		$TimeR = '1 Hour Few Minutes';
    	}
    	elseif($MyTime < '60' && $MyTime > '0'){
    		$TimeR = $MyTime .' Minutes';
    	}
    	elseif($MyTime < '1'){
    		$TimeR = 'Few Seconds';
    	}
    echo '<div class="warning"><i class="fa fa-warning"> </i> Your Accout will be Deleted if You Fail To Pay before 24 hours
    (<font color="green"><i class="fa fa-clock-o"> </i> '.$TimeR.'</font>) Left! ';
    include '../count.php';
    echo '</div><br/>

  5. #5
    Join Date
    Dec 2012
    Posts
    1,283
    this code to 24hours auto purge instead of this 1hr
    Can you explain what you mean when writing "auto purge"? English is not my mother tongue and my dictionary says that "purge" means something like "cleanup". What does this mean in a coding context?

  6. #6
    Join Date
    Apr 2017
    Posts
    10
    Quote Originally Posted by Sempervivum View Post
    Can you explain what you mean when writing "auto purge"? English is not my mother tongue and my dictionary says that "purge" means something like "cleanup". What does this mean in a coding context?
    Yes it's clean out

    Code:
     Please help me look into this code and correct it to 24hours instead of 1hour. 
    
    Thanks 
    
    <!-- begin snippet: js hide: false -->
    
    <!-- language: lang-js -->
    <script>
        function startTimer(duration, display) {
            var start = Date.now(),
                diff,
                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
                minutes = (diff / 60 * 60) | 0;
                seconds = (diff % 60) | 0;
    
                minutes = minutes < 10 ? "0" + minutes : minutes;
                seconds = seconds < 10 ? "0" + seconds : seconds;
    
                display.textContent = hours + ":" + minutes + ":" + seconds; 
    
                if (diff <= 0) {
                    // add one second so that the count down starts at the full duration
                    // example 05:00 not 04:59
                    start = Date.now() + 1000;
                }
            };
            // we don't want to wait a full second before the timer starts
            timer();
            setInterval(timer, 1000);
        }
    
        window.onload = function () {
            var fiveMinutes = 60 * 2,
                display = document.querySelector('#time');
            startTimer(fiveMinutes, 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

  7. #7
    Join Date
    Apr 2017
    Posts
    10
    Quote Originally Posted by \\.\ View Post
    That is just over blown code.

    You have a serious flaw in the timer() function...
    Code:
    // we don't want to wait a full second before the timer starts
    timer();
    setInterval(timer, 1000);
    }
    Completely the wrong way to use setInterval.
    You use it like this
    Code:
    }
    // put setInterval outside of the function and give it a variable so you can clearInterval at a later date if needed.
    timerHandler = setInterval( timer, 1000);
    so it is outside of the function, not in it and calling the fimer function without "" or () will call it immediately.

    This is an example of a simple countdown clock, it counts down to midnight of the current day
    Code:
    <div id="midnightcountdown"><script>
    	d = new Date();
    	midnightHour = new Date(d.getFullYear(),d.getMonth()+1,d.getDate(),23,59,59); // todays date
    	midnight = {
    		target:false,
    		end:midnightHour.getTime(),
    		tick:function(){
    			this.now = new Date();
    			if( (this.now.getTime() < this.end ) ){
    				this.remain = new Date( this.end - this.now.getTime() ).toUTCString().slice(16,25);
    				}else this.remain = "00:00:00"; //<-- this is where your purge widget would go to trigger the purge.
    			if( this.target ) this.target.innerHTML = "Left : " + this.remain;
    		},
    		init:function(){
    			if( !this.target ) this.target = document.getElementById("midnightcountdown");
    			if( this.target ){
    				clearInterval(this.auto);
    				this.auto = setInterval("midnight.tick()",500);
    				
    			}  
    		},
    		auto:setInterval("midnight.init()",999)
    	}
    </script></div>
    What you don't need to do is track or calculate hours, minutes, do any padding by using the UTC string method, you can chop out the time, get a date reference that can be used in a date object and as shown, get a countdown by using the date object to calculate the remaining time and grab a ready formatted string.

    This timer is based on the user local machine, if you want a countdown based on the server and its location, then you need to use a bit of PHP to facilitate grabbing this information and setting the timer to display time left on the server irrespective of timezone. As you have not said, its assumed that the clients timezone is the relevant one.
    Pls I don't understand this javascript just please be of help and help me re right the script Pls.

    The time you gave me along ain't working

    Code:
     <!-- begin snippet: js hide: false -->
    
    <!-- language: lang-js -->
    <script>
        function startTimer(duration, display) {
            var start = Date.now(),
                diff,
                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
                minutes = (diff / 60 * 60) | 0;
                seconds = (diff % 60) | 0;
    
                minutes = minutes < 10 ? "0" + minutes : minutes;
                seconds = seconds < 10 ? "0" + seconds : seconds;
    
                display.textContent = minutes + ":" + seconds; 
    
                if (diff <= 0) {
                    // add one second so that the count down starts at the full duration
                    // example 05:00 not 04:59
                    start = Date.now() + 1000;
                }
            };
            // we don't want to wait a full second before the timer starts
            timer();
            setInterval(timer, 1000);
        }
    
        window.onload = function () {
            var fiveMinutes = 60 * 2,
                display = document.querySelector('#time');
            startTimer(fiveMinutes, 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

  8. #8
    Join Date
    Dec 2012
    Posts
    1,283
    If I understand your first posting correctly, you want to change the initial value of the timer to 24 hours? And does "auto purge" mean that the timer should not start again when it has run out? Then this should do the job:
    Code:
        <script>
            function startTimer(duration, display) {
                var start = Date.now(),
                diff,
                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 * 2,
                    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
    Note that it's improbable that the user keeps his browser open for 24 hours. You will have to store the starting time in local storage or on the server.
    Last edited by Sempervivum; 04-30-2017 at 05:28 PM.

  9. #9
    Join Date
    Apr 2017
    Posts
    10
    Quote Originally Posted by Sempervivum View Post
    If I understand your first posting correctly, you want to change the initial value of the timer to 24 hours? And does "auto purge" mean that the timer should not start again when it has run out? Then this should do the job:
    Code:
        <script>
            function startTimer(duration, display) {
                var start = Date.now(),
                diff,
                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 * 2,
                    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
    I will test this and get back to you thanks

  10. #10
    Join Date
    Mar 2007
    Location
    localhost
    Posts
    4,503
    Works fine on my PC.
    --> JavaScript Frameworks like JQuery, Angular, Node <--
    ... and please remember to wrap code with forum BBCode tags:-

    [CODE]...[/CODE] [HTML]...[/HTML] [PHP]...[/PHP]

    If you can't think outside the box, you will be trapped forever with no escape...

  11. #11
    Join Date
    Apr 2017
    Posts
    10
    Quote Originally Posted by Sempervivum View Post
    If I understand your first posting correctly, you want to change the initial value of the timer to 24 hours? And does "auto purge" mean that the timer should not start again when it has run out? Then this should do the job:
    Code:
        <script>
            function startTimer(duration, display) {
                var start = Date.now(),
                diff,
                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 * 2,
                    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
    Note that it's improbable that the user keeps his browser open for 24 hours. You will have to store the starting time in local storage or on the server.
    Thank you very much

    What needs change here Pls to make it write 24houra because it's written 18min if I put 1 where there is 60 value I mean the first 60





    Code:
     <h2>MERGED DETAILS!</h2></div></div>
    <div class="fa-boardx" style="width:90%;border:2px solid grey;"></div>
    <h5>You Are making payment to:</h5>';
    if($set['autopurge'] > 0){
    	$MyTime = ceil(($mergto['xtime'] - time())/1);
    	if($MyTime >= '1440' && $MyTime > '1380'){
    		$TimeR = '24 Hours Few Minutes';
    	}
    	elseif($MyTime >= '1380' && $MyTime > '1320' && $MyTime < '1440'){
    		$TimeR = '23 Hours Few Minutes';
    	}
    	elseif($MyTime >= '1320' && $MyTime > '1260' && $MyTime < '1380'){
    		$TimeR = '22 Hours Few Minutes';
    	}
    	elseif($MyTime >= '1260' && $MyTime > '1200' && $MyTime < '1320'){
    		$TimeR = '21 Hours Few Minutes';
            }
    	elseif($MyTime >= '1200' && $MyTime > '1140' && $MyTime < '1260'){
    		$TimeR = '20 Hours Few Minutes';
    	}
    	elseif($MyTime >= '1140' && $MyTime > '1080' && $MyTime < '1200'){
    		$TimeR = '19 Hours Few Minutes';
    	}
    	elseif($MyTime >= '1080' && $MyTime > '1020' && $MyTime < '1140'){
    		$TimeR = '18 Hours Few Minutes';
    	}
    	elseif($MyTime >= '1020' && $MyTime > '960' && $MyTime < '1080'){
    		$TimeR = '17 Hours Few Minutes';
    	}
    	elseif($MyTime >= '960' && $MyTime > '900' && $MyTime < '1020'){
    		$TimeR = '16 Hours Few Minutes';
    	}
    	elseif($MyTime >= '900' && $MyTime > '840' && $MyTime < '960'){
    		$TimeR = '15 Hours Few Minutes';
            }
    	elseif($MyTime >= '840' && $MyTime > '780' && $MyTime < '900'){
    		$TimeR = '14 Hours Few Minutes';
    	}
    	elseif($MyTime >= '780' && $MyTime > '720' && $MyTime < '840'){
    		$TimeR = '13 Hours Few Minutes';
    	}
    	elseif($MyTime >= '720' && $MyTime > '660' && $MyTime < '780'){
    		$TimeR = '12 Hours Few Minutes';
    	}
    	elseif($MyTime >= '660' && $MyTime > '600' && $MyTime < '720'){
    		$TimeR = '11 Hours Few Minutes';
    	}
    	elseif($MyTime >= '600' && $MyTime > '540' && $MyTime < '660'){
    		$TimeR = '10 Hours Few Minutes';
    	}
    	elseif($MyTime >= '540' && $MyTime > '480' && $MyTime < '600'){
    		$TimeR = '9 Hours Few Minutes';
    	}
    	elseif($MyTime >= '480' && $MyTime > '420' && $MyTime < '540'){
    		$TimeR = '8 Hours Few Minutes';
    	}
    	elseif($MyTime >= '420' && $MyTime > '360' && $MyTime < '480'){
    		$TimeR = '7 Hours Few Minutes';
    	}
    	elseif($MyTime >= '360' && $MyTime > '300' && $MyTime < '420'){
    		$TimeR = '6 Hours Few Minutes';
    	}
    	elseif($MyTime >= '300' && $MyTime > '240' && $MyTime < '360'){
    		$TimeR = '5 Hours Few Minutes';
    	}
    	elseif($MyTime >= '240' && $MyTime > '180' && $MyTime < '300'){
    		$TimeR = '4 Hours Few Minutes';
    	}
    	elseif($MyTime >= '180' && $MyTime > '120' && $MyTime < '240'){
    		$TimeR = '3 Hours Few Minutes';
    	}
    	elseif($MyTime >= '120' && $MyTime > '60' && $MyTime < '180'){
    		$TimeR = '2 Hours Few Minutes';
    	}
    	elseif($MyTime >= '60' && $MyTime < '120'){
    		$TimeR = '1 Hour Few Minutes';
    	}
    	elseif($MyTime < '60' && $MyTime > '0'){
    		$TimeR = $MyTime .' Minutes';
    	}
    	elseif($MyTime < '1'){
    		$TimeR = 'Few Seconds';
    	}
    echo '<div class="warning"><i class="fa fa-warning"> </i> Your Accout will be Deleted if You Fail To Pay before 24 hours
    (<font color="green"><i class="fa fa-clock-o"> </i> '.$TimeR.'</font>) Left! ';
    include '../count.php';
    echo '</div><br/>

  12. #12
    Join Date
    Dec 2012
    Posts
    1,283
    What needs change here Pls to make it write 24houra because it's written 18min if I put 1 where there is 60 value I mean the first 60
    I really have problems to understand what your are posting. However when I look at your code, this might meet your intention:
    Code:
        <script>
            function startTimer(duration, display) {
                var start = Date.now(),
                diff,
                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;
    
                    var additionalText = " ";
                    if (diff < 60 * 60) {
                        additionalText += minutes + " minutes left";
                    } else {
                        additionalText += hours + " hours and a few minutes left";
                    }
    
                    //var additionalText = " ";
                    //if (diff < 60) {
                    //    additionalText += seconds + " minutes left";
                    //} else {
                    //    additionalText += minutes + " 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) {
                        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 * 2,
                    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

  13. #13
    Join Date
    Mar 2007
    Location
    localhost
    Posts
    4,503
    See this bit...
    Code:
    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;
    You don't need different variables or use padding to get a formatted output
    Code:
    // set up an end time 
    endTime = new Date("2017-12-31 23:59:59").getTime();
    // time left is difference beteween endTime and nowTime
    remain = new Date( endTime - timeNow.getTime() );
    // get the Hours, Minutes and Seconds as a ready formatted string
    hms = remain.toUTCString().slice(16,25);
    // update the display.
    display.textContent = hms;
    The best way of setting up a countdown timer is to create a futureDate and not calculate for a futureDate by amount of time passed, use how much time is left instead...
    --> JavaScript Frameworks like JQuery, Angular, Node <--
    ... and please remember to wrap code with forum BBCode tags:-

    [CODE]...[/CODE] [HTML]...[/HTML] [PHP]...[/PHP]

    If you can't think outside the box, you will be trapped forever with no escape...

  14. #14
    Join Date
    Dec 2012
    Posts
    1,283
    The best way of setting up a countdown timer is to create a futureDate
    Hallo //./
    I can't see any benefit in doing so. A fact is that many countdowns are required to count down to a fixed date in the future and in that case it is a suitable approach. However the task of the TO is different: He needs a countdown for the remaining time for a fixed interval.

  15. #15
    Join Date
    Mar 2007
    Location
    localhost
    Posts
    4,503
    Exactly and thats what I illustrated without the need for padding routines and using calculations or having to put together a string.

    For a future time reference as the end point of the timeout, your only test need be to see that current time is not greater than the future date.

    Much simpler.
    --> JavaScript Frameworks like JQuery, Angular, Node <--
    ... and please remember to wrap code with forum BBCode tags:-

    [CODE]...[/CODE] [HTML]...[/HTML] [PHP]...[/PHP]

    If you can't think outside the box, you will be trapped forever with no escape...

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