Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Aug 2010
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Cool Advanced countdown timer

    Hi there,

    Hopefully someone will be able to help me here. I found the below script that works fine for the actual countdown, but when the user refreshes the page, the countdown starts from 5 minutes again.

    What I am trying to do, is handle users that put something in their shopping cart and then get side tracked or what ever reason and that same product then being purchased by another person when I only have 1 left.

    So using PHP I am going to subtract the qty of that product by 1 and then I want the website to either put it back, if the users checkout times out, or if a normal process it is taken out permanently.

    So long story short I think I need some sort of 'memory' for the javascript, so the user can't just keep refreshing and it starting from 5 minutes each time. Also it needs to be carried through to other pages.

    Your help is greatly appreciated.

    Thanks,
    Simon.



    Code:
    <script type="text/javascript">
    /* This script and many more are available free online at
    The JavaScript Source :: http://javascript.internet.com
    Created by: Neill Broderick :: http://www.bespoke-software-solutions.co.uk/downloads/downjs.php */
    
    var mins
    var secs;
    
    function countdown() {
     	mins = 1 * CDm("5"); // change minutes here
     	secs = 0 + CDs(":01"); // change seconds here (always add an additional second to your total)
     	CDredo();
    }
    
    function CDm(obj) {
     	for(var i = 0; i < obj.length; i++) {
      		if(obj.substring(i, i + 1) == ":")
      		break;
     	}
     	return(obj.substring(0, i));
    }
    
    function CDs(obj) {
     	for(var i = 0; i < obj.length; i++) {
      		if(obj.substring(i, i + 1) == ":")
      		break;
     	}
     	return(obj.substring(i + 1, obj.length));
    }
    
    function CDdis(mins,secs) {
     	var disp;
     	if(mins <= 9) {
      		disp = " 0";
     	} else {
      		disp = " ";
     	}
     	disp += mins + ":";
     	if(secs <= 9) {
      		disp += "0" + secs;
     	} else {
      		disp += secs;
     	}
     	return(disp);
    }
    
    function CDredo() {
     	secs--;
     	if(secs == -1) {
      		secs = 59;
      		mins--;
     	}
     	document.getElementById("countdown").firstChild.nodeValue = CDdis(mins,secs);
    	//document.cd.disp.value = dis(mins,secs); // setup additional displays here.
     	if((mins == 0) && (secs == 0)) {
      		window.alert("Time is up. Press OK to continue."); // change timeout message as required
      		// window.location = "yourpage.htm" // redirects to specified page once timer ends and ok button is pressed
     	} else {
     		countdown = setTimeout("CDredo()",1000);
     	}
    }
    
    function coundowninit() {
      countdown();
    }
    </script>
    Code:
    <span id="countdown">&nbsp;</span>

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,731
    Thanks
    202
    Thanked 2,508 Times in 2,486 Posts
    Quote Originally Posted by simontux View Post
    I found the below script that works fine for the actual countdown, but when the user refreshes the page, the countdown starts from 5 minutes again.
    Well, if the page is refreshed that is what will happen. You could perhaps set a cookie, or better use server-side scripting. Remember that some people disable Javascript in their browsers so any Javascript solution will not be reliable. And obviously a Javascript count-down will not work if Javascript is disabled.

    You need to check the availability of the product at the moment of checkout to avoid the one single item being purchased by two customers.

    All advice is supplied packaged by intellectual weight, and not by volume. Contents may settle slightly in transit.


  •  

    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
    •