Home > General > Affiliate Marketing Forum

Help with a script please (8)


03-08-2014 04:11 AM #1 bravenewworld (Member)
Help with a script please

I want to have a decreasing "spots remaining" script. Here is what I have so far. But the problem is that it goes negative. I want it to go down to 4 or 5 spots remaining and stop there.


<script type="text/javascript">

// This is in milliseconds
var delay_seconds = 12000
var female_count = 100

setInterval(function() {
// This is maximum number to increment count by
var increment_max = 1

// Get random number between 1 and increment_max
var rand_num = Math.floor(Math.random() * increment_max) + 1

// Add random number to current count
female_count -= rand_num

document.getElementById("random_number").innerHTML = female_count
}, delay_seconds)

</script>


03-08-2014 04:23 AM #2 zeno (Administrator)

Try this, it is a randomised countdown, stops at 1.

Code:
<!DOCTYPE HTML>
<html>
<head><title>Countdown</title></head>    
<body>
<script type="text/javascript">
    var count = 100;
    function countdown() {
        count -= 1;
        if( count <= 0) {
            count = 0;
            xlearInterval(timer);    
        }};
    (function loop() {
        var rand = Math.round(Math.random() * (1000)) + 500;
        setTimeout(function() {
            countdown();
            loop();  
            }, rand);
document.getElementById('seconds').innerHTML = count;
}());
</script>
<div id="seconds">100</div>
</body>
</html>
var count = the starting value from which it then subtracts.
The math.random part... in brackets you have the delay in ms * a random number, the +500 just adds length to the whole thing.
Then the javascript acts on the div with id="seconds". You need to have your starting number in here as the javscript changes its value but doesn't give it one to begin - so it starts blank unless you put a number in the div.


03-08-2014 06:13 AM #3 bravenewworld (Member)

How can I slow it down? It's decreasing really quickly


03-08-2014 06:32 AM #4 swiftclick (Senior Member)

i just modified zeno's script

starts at 12, counts down by 1 at a random rate of speed between 400-4000 milliseconds, and stops at 4

Code:
<script type="text/javascript">
    var count = 12;
    function countdown() {
        count -= 1;
        if( count <= 3) {
            count = 3;
            xlearInterval(timer);    
        }};
    (function loop() {
        var rand = Math.round(Math.random() * (4000)) + 400;
        setTimeout(function() {
            countdown();
            loop();  
            }, rand);
document.getElementById('count').innerHTML = count;
}());
</script>

<div id="count">12</div>


03-08-2014 06:44 AM #5 bravenewworld (Member)

Nice! Thanks man.

Last thing, how can I have them on the same line? I want to have 100 Spots Remaining right next to each other...currently it is 100 and spots remaining on the next line.

<div id="seconds">100</div>
Spots Remaining


03-08-2014 07:50 AM #6 snipe (Member)

Just change div to span!

Code:
<span id="seconds">100</span> Spots Remaining


03-08-2014 07:54 AM #7 zeno (Administrator)

Try something like 100 spots remaining

^^ lol thanks forum for evaluating that markup.

Code:
<div><span><span id="seconds">100</span> spots remaining</span></div>


03-08-2014 07:55 AM #8 bravenewworld (Member)

Awesome! Thanks snipe


Home > General > Affiliate Marketing Forum