Javascript Help

judif414

Regular Member
Joined
Feb 25, 2013
Messages
487
Reaction score
444
Hey guys hope some programmers can help out.

I need a javascript counter that can count from a number I specify (ex. 435) and update every 1-4 seconds (randomly).

Also needs to increase by 1. Would like to add some text in there as well if possible.

Much appreciated!
 
Hey there mate

For this task you would do good in using setTimeout() function (http://www.w3schools.com/jsref/met_win_settimeout.asp), where you will grab the html element and set the new value. For example:

var counter = 0;

var timeoutVar;

var intervalFunct = function(){
counter++;
$("a").text("Counter: "+counter);
var intervalTime = Math.floor((Math.random() * 4000) + 1000);
timeoutVar = setTimeout(intervalFunct, intervalTime);
}

timeoutVar = setTimeout(intervalFunct, 1000);

You need to change the use jQuery, change the jQuery selector and choose the starting counter.

Cheers
 
Hey there mate

For this task you would do good in using setTimeout() function (http://www.w3schools.com/jsref/met_win_settimeout.asp), where you will grab the html element and set the new value. For example:



You need to change the use jQuery, change the jQuery selector and choose the starting counter.

Cheers

Thanks man, looks good. I just don't know how to implement it with html.

Can anyone else help?

I'm trying to implement the above code into a simple html landing page.
 
Hey,

It depends on the kind of site and what you're specifically trying to accomplish. I know for sure there are a lot of free jQuery plugins that do what you ask for for free.
 
Hey,

It depends on the kind of site and what you're specifically trying to accomplish. I know for sure there are a lot of free jQuery plugins that do what you ask for for free.

Gotcha.

This is what I want to do exactly.

I want the counter to go up (like I described in the first post) instead of being static like this:

Code:
<span class="downloads">531 Downloads<img src="img/downloadimage.png" alt="downloads" />

BTW, it's just an html template, nothing fancy.

Thanks again for the help!
 
Oh, OK,

Then just add this up:

<script>
$(document).ready(function(){
var counter = 0;

var timeoutVar;

var intervalFunct = function(){
counter++;
$(".downloads").text(counter + " Downloads");
var intervalTime = Math.floor((Math.random() * 4000) + 1000);
timeoutVar = setTimeout(intervalFunct, intervalTime);
}
timeoutVar = setTimeout(intervalFunct, 1000);

});
</script>

Hope it helps
 
Thanks again, just not sure where to put the code? Do I put it inside the <span> </span> tags?

Thanks
 
No, just anywhere before you close the <body> tag, outside of any other tag, other than <script>
 
No, just anywhere before you close the <body> tag, outside of any other tag, other than <script>

AWESOME, got it to work! Wow appreciate all the help!
Thanks!
 
Back
Top