Home > Programming, Servers & Scripts >

A Customisable Countdown Script For Your Landers (32)


08-16-2013 03:31 PM #1 caurmen (Administrator)
A Customisable Countdown Script For Your Landers

If there's one script that you'll use again and again in your landers, it's a countdown thread. They're a quick solution to the perennial scarcity problem - how to force readers to take action NOW!

Whether you want to tell people that there are [3] invitations remaining or to let them know that 353 - no, wait, 354 - people have signed up today, this script will let you do that. It'll remember the number if they refresh, and count down at a customisable rate.

This one isn't all my work, by any means. The collective brains of Stack That Money came up with this script a while ago, so rather than let it get buried, I thought I'd comment it up and post it as a sticky thread.

The Script Itself

Paste this div wherever you want the countdown to happen in your page:

Code:
<div id="spaces" style="font-size:20px;">loading..</div>
Then, paste this at the bottom of your lander, just above the </body> tag:

Code:
<script>
        var minSpaces = 5; //Minimum spaces to start with
        var maxSpaces = 10; //Maximum spaces to start with
        var maxDecTime = 6000; //Max time interval between decrements
         var minDecTime = 300; //Min time interval between decrements
        var redirectWhenDone = 0; //Redirect = 1 set to 0 for no redirect
        var stopSpaces = 3; //Number it will stop at if not using redirect
        var redirectLocation = 'http://www.google.com';

        if(readCookie("countdown")) {
		document.write (document.cookie); 
            maxSpaces = parseInt(readCookie("countdown"));
            minSpaces = Math.max(maxSpaces-5, 1);
        }
        var spaces = Math.floor(Math.random()*(maxSpaces-minSpaces+1)+minSpaces);

function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}        

function updateSpaces() {
            spaces--;
            createCookie ("countdown", parseInt(spaces), 14);
            document.getElementById('spaces').innerHTML = 
                '<span style="color:orange;">('+spaces+')</span> orders left!';
            var intvl = Math.round(Math.random()*maxDecTime) + minDecTime;
            if(spaces>stopSpaces){
                setTimeout(updateSpaces, intvl);
            }
           else {//No spaces left, redirect!
                if(redirectWhenDone==1) {
                    window.top.location = redirectLocation;
                }
    }}
        window.onload=updateSpaces();
    </script>
Customising The Script

Changing the text

To edit what the countdown says, just edit the

Code:
 '<span style="color:orange;">('+spaces+')</span> orders left!';
line. For example, you could edit it to

Code:
 '<span style="color:orange;">['+spaces+']</span> invitations remaining!';
That'll change the output from (for example) "(3) orders left" to "(3) invitations remaining".

You can also edit the HTML in there as usual. For example, if you want the entire line to be in orange, change it to

Code:
 '<span style="color:orange;">('+spaces+') orders left!</span>';
Changing the numbers

You can change the maximum and minimum starting values by changing the numbers after minSpaces and MaxSpaces. You can insert any whole number here, from 1 to 1,000,000.

You can change how frequently it updates by changing maxDecTime and minDecTime - they're in milliseconds, so 1000 = 1 second. The script randomly chooses between times between these two.

If you want the script to always update at the same time, set maxDecTime very low - 20 or so. This minimises the randomness.

To change the number at which the script will redirect the visitor (if it's set to do that - see below), change stopSpaces.

Finally, if you want the counter to count up, not down, change "spaces--;" to "spaces++;".

Redirects

If you want the visitor to be redirected once the counter reaches a minimum number of spaces, change

Code:
        var redirectWhenDone = 0;
to

Code:
        var redirectWhenDone = 1;
You can set the location that they will be redirected to in

Code:
        var redirectLocation = 'http://www.google.com';
- simply replace http://www.google.com with the URL you want to redirect to.

That's it! I hope that was helpful - if you're confused, have suggestions, are having problems or have any other comment, please do post it below!


08-16-2013 04:47 PM #2 groomez (Veteran Member)

fuuuuuuuuuuuuuuuuuuuuuuuuuck yea


09-04-2013 10:53 AM #3 cyberdelicstudio (Member)

Big up!


09-04-2013 11:31 AM #4 nebuer (Member)

thank you!


09-04-2013 11:41 AM #5 redrummr (Member)

Do you even pay for your own drinks at STM meetups? Add me to the beers-owed list.


09-24-2013 09:08 AM #6 suipowers (Member)

Hey bro, i use this code on beyondhosting, it show the mistake:

(0) orders left!
PHPSESSID=e225a7b3bfc0b538ba036128dbdd1c48; LoginPage=login.php; countdown=0

what happened? Thank you very much!


09-25-2013 07:49 AM #7 suipowers (Member)

Hey caurmen, I'm pretty new here and I found when I refresh the page every time, the page will show one code: "countdown=x" (x is one number). Just use the <span style="visibility:hidden;"><script>...</script></span> to hid the code, that is very perfect!


09-25-2013 10:38 AM #8 caurmen (Administrator)

Cool, glad you found a way around it!


04-25-2014 01:09 PM #9 mrclifton_ (AMC Alumnus)

Great share, thanks caurmen!

I have a slight problem though (still a newbie when it comes to javascript).

This script works perfect when I preview it locally, but as soon as I upload it to the server the counter is usually set to zero and I get a string like this at the bottom of the page:

_ga=GA1.2.712959500.1398338702; countdown=0; wfvt_381747506=535a5b420036b

I'm going crazy about this, as you understand this really KILLS my CTR, hard to profit with a 4% LP CTR, lol.

Would really appreciate some help.


04-26-2014 01:51 AM #10 zeno (Administrator)

Odd - javascript is all client side so if it works locally it should work online.

Is the page you uploaded identical to that which you tested locally? Do you have any server-side programs running that automatically inject code or modify page content? Do you have cookies enabled?


09-20-2014 05:28 PM #11 barnone (Member)

If you built your lander with Muse, do you insert the script in Muse via Object>Insert Html

Or do you just insert the code after exporting the files to your desktop

Having trouble inserting the code without it affecting other parts of the page...


09-21-2014 06:24 AM #12 zeno (Administrator)

Always do code inserts post-export from Muse. You need to know exactly where the code is going.

The script part won't effect the page at all.

To get the functionality, there is no need to add another div - just design your lander in Muse to include the text that you want to replace with the countdown.

Then change that div/span's id to "spaces" in the code, and also go into the CSS and change the div's original id to spaces, and you will be good to go.


02-03-2015 04:30 PM #13 avn_0903 (Member)

Is there a countdown timer version of this?


05-04-2015 02:45 PM #14 omrikos (Member)

Great Timer!

How can I remove the "(x)" from the number so it will seem like a natural part of the text?

For example:
instead of:
Update in (14) seconds
It will be:
Update in 14 seconds?

Never used this timer in the past, I like the redirect at the end and the ability to plant cookies.

Amazing!


05-04-2015 04:02 PM #15 caurmen (Administrator)

If you want to remove the brackets, you can just take them out of the span in the text - it should still work fine!

So you'd just edit to

Code:
 '<span style="color:orange;"> '+spaces+' orders left!</span>';
, for example.


05-31-2015 07:06 AM #16 seapanda (Member)

Have a question, hope someonecan assist me with this, on my localhost it works perfectly - uploaded it to RackSpace but it does not work:

Placed the following code my lander:

Code:
<div id="spaces" style="font-size:18px;">loading...</div>
Code:
<script>
        var minSpaces = 8; //Minimum spaces to start with
        var maxSpaces = 13; //Maximum spaces to start with
        var maxDecTime = 9000; //Max time interval between decrements
         var minDecTime = 1000; //Min time interval between decrements
        var redirectWhenDone = 0; //Redirect = 1 set to 0 for no redirect
        var stopSpaces = 4; //Number it will stop at if not using redirect
        var redirectLocation = 'http://www.google.com';

        if(readCookie("countdown")) {
        document.write (document.cookie); 
            maxSpaces = parseInt(readCookie("countdown"));
            minSpaces = Math.max(maxSpaces-5, 1);
        }
        var spaces = Math.floor(Math.random()*(maxSpaces-minSpaces+1)+minSpaces);

function createCookie(name,value,days) {
    if (days) {
        var date = new Date();
        date.setTime(date.getTime()+(days*24*60*60*1000));
        var expires = "; expires="+date.toGMTString();
    }
    else var expires = "";
    document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');
    for(var i=0;i < ca.length;i++) {
        var c = ca[i];
        while (c.charAt(0)==' ') c = c.substring(1,c.length);
        if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
    }
    return null;
}        

function updateSpaces() {
            spaces--;
            createCookie ("countdown", parseInt(spaces), 14);
            document.getElementById('spaces').innerHTML = 
                ' Only <span style="color:red;">('+spaces+')</span> New Registrations Left!';
            var intvl = Math.round(Math.random()*maxDecTime) + minDecTime;
            if(spaces>stopSpaces){
                setTimeout(updateSpaces, intvl);
            }
           else {//No spaces left, redirect!
                if(redirectWhenDone==1) {
                    window.top.location = redirectLocation;
                }
    }}
        window.onload=updateSpaces();
    </script>
Uploaded to Rackspace give me:

"Only (0) New Registrations Left!" and countdown=0 at the bottom of the page...

Hope someone can assist me with this, I'm not very technical

*update* - checked this via my mobile phone and it's working, the "Only (9) New Registrations Left!" part , this makes it even more odd...

Could this have to do with the Cookie Session?


05-31-2015 09:07 AM #17 diltsi (Member)

The sad thing is that neither my affiliate network or traffic source allows countdown scripts :S.


06-08-2015 10:13 PM #18 rudy_z (Member)

Hi!
I'm also having problem with the script after uploading it to the server. It works perfectly well when viewing locally AND online in incognito mode (with cookies disabled) but it just shows '(0)' when the cookies are enables. Any solution?
Thanks!


06-11-2015 03:57 PM #19 caurmen (Administrator)

In both cases, I'd recommend clearing your cookies and trying again - sounds like it might be getting confused because you have a cookie already set.

Let me know if that doesn't work!


06-11-2015 04:03 PM #20 hlyghst ()

Quote Originally Posted by diltsi View Post
The sad thing is that neither my affiliate network or traffic source allows countdown scripts :S.
it doesn't have to count downwards.......


06-12-2015 01:30 AM #21 adsflo (Member)

Wow that's thinking out of the box. Thanks for that tip!


11-09-2015 04:54 AM #22 frshprince (AMC Alumnus)

Hey i just tried this script for my mobile lander.. It just says "loading.." ANy solution to this? Thanks in advance!


11-09-2015 05:08 AM #23 Adamw (AMC Alumnus)

Quote Originally Posted by frshprince View Post
Hey i just tried this script for my mobile lander.. It just says "loading.." ANy solution to this? Thanks in advance!
Hey man... that's my favorite show (fresh prince), can recite every episode word for word, not sure if I should really be proud of this or not lol... but to help out with your Q...

load it up on chrome on your laptop, right click -> inspect element -> top right corner will be a red stop sign looking icon with a number next to it, this is your errors on the page load (screen shot reference) -> click the icon, shows what your error is -> paste back what your error is here

Then we'd be able to give you better direction.


11-09-2015 04:03 PM #24 frshprince (AMC Alumnus)

Quote Originally Posted by Adamw View Post
Hey man... that's my favorite show (fresh prince), can recite every episode word for word, not sure if I should really be proud of this or not lol... but to help out with your Q...

load it up on chrome on your laptop, right click -> inspect element -> top right corner will be a red stop sign looking icon with a number next to it, this is your errors on the page load (screen shot reference) -> click the icon, shows what your error is -> paste back what your error is here

Then we'd be able to give you better direction.
Hey thanks for the helping hand.. When i have the code in my dreamweaver it doesn't pick up any errors..


11-09-2015 04:03 PM #25 frshprince (AMC Alumnus)

i deleted the script but i will try again


11-09-2015 04:58 PM #26 frshprince (AMC Alumnus)

Click image for larger version. 

Name:	Screen Shot 2015-11-09 at 12.57.28 PM.png 
Views:	82 
Size:	302.2 KB 
ID:	8957
i applied to script as suggested and this is what i get?


11-11-2015 09:47 AM #27 caurmen (Administrator)

@frshprince - can you PM me the URL of the lander? I'll have a look at it and PM you back with suggestions. At this point I think we've got to look at the implementation live on the site.


04-07-2016 06:16 PM #28 spicyprinter (Member)

This is great! I love that it stores the countdown.

The only problem is when I refresh the page, a random error always pops below the number:
_gat_as25n45=1; countdown=60; _ga=GA1.2.980150996.1460052886

Anyone found a solution to this?


04-08-2016 10:51 AM #29 caurmen (Administrator)

@spicyprinter - Hmm, that's weird. On all browsers, or just one?


06-19-2020 09:57 AM #30 affguru (Member)

Hi caurmen, thanks for sharing , how i can insert it in elementor page builder ? thank you!


06-19-2020 10:03 AM #31 larsometer (Senior Member)

Best way is to ask elementor where to place custom code. I guess there is a code box / element in the set in which you just paste it. At least that is how it works with some other page builders.


06-21-2020 10:08 PM #32 jeremie (Moderator)

Quote Originally Posted by affguru View Post
Hi caurmen, thanks for sharing , how i can insert it in elementor page builder ? thank you!
@affguru, Caurmen unfortunately died 2 years ago
https://stmforum.com/forum/showthrea...rmenampampquot

You need to use HTML elements. See:
https://www.youtube.com/watch?v=_E1KgTDvUeU
https://developers.elementor.com/cre...entor-widgets/


Home > Programming, Servers & Scripts >