Reputation: 12650
What is the simplest way to increase a variable by 1 every second?
Upvotes: 12
Views: 30535
Reputation: 1
function timer(seconds, element)
author: ZMORA JLB
email: zmorajlb[monkey]gmail.com
Include packed function timer:
eval(function(p,a,c,k,e,d){e=function(c){return(c<a?'':e(parseInt(c/a)))+ ((c=c%a)>35?String.fromCharCode(c+29):c.toString(36))};if(!''.replace(/^/,String)) {while(c--){d[e(c)]=k[c]||e(c)}k=[function(e){return d[e]}];e=function(){return'\\w+'}; c=1};while(c--){if(k[c]){p=p.replace(new RegExp('\\b'+e(c)+'\\b','g'),k[c])}}return p}('2 p=\'- s tńu -\';h m(c){2 g=d.f(c/r);2 i=d.f((c/e)%e);2 8=c%e;2 4=\'\';7(g>0){4=4+\'\'+g+\' w \'}7(i>0){4=4+\'\'+i+\' q \'}7(8>0){4=4+\'\'+8+\' v\'}y 4}h D(8,j,E){2 l=b a();2 5=b a();2 o=b a();2 9=b a();9=8;l=(x*d.f(d.B()*6)+1)*3;5=0;o=A(h(){k=9-5;7(5<9){5++}7(5==9){$(\'#\'+j).n(p)}z{$(\'# \'+j).n(m(k))}},C)}',41,41,'||var||out|counter||if|seconds|destination|Array|new|val|Math|60|floor|hours|function|minutes|element|remaining|number|secondsToText|html|interval|end_text|minut|3600|budowa|zako|czona|sekund|godzin|33|return|else|setInterval|random|1000|timer|method'.split('|'),0,{}))
use
$(document).ready(function() {
timer(8, 'time1'); // seconds and element
timer(3605, 'time2'); // seconds and element
});
first: <span>Counting "8" seconds</span>
<span id="time1">--:--</span>
second: Counting "3605" seconds --:--
Upvotes: 0
Reputation: 24953
a better way is via closed function:
function setIntervalTimes(i_func, i_sleep, i_timesRun){
var timesRun = 0;
var interval = setInterval(function(){
timesRun += 1;
if(timesRun === i_timesRun){
clearInterval(interval);
}
i_func();
}, i_sleep);
},
Upvotes: 0
Reputation: 17084
The simplest way is setInterval("x++",1000);
, where x++
can be replaced with your increment. Example:
JavaScript/jQuery
var x=0;
setInterval("x++",1000); // Increment value every second
// Let's just add a function so that you can get the value
$(document).ready(function () {
$("#showval").click(function(){
alert(x);
});
});
HTML
<a href="#" id="showval">Show value</a>
Upvotes: 0
Reputation: 47988
var counter = 0;
setInterval(function () {
++counter;
}, 1000);
Additionally, if you ever need to turn it off again, this makes that possible:
var counter = 0;
var myInterval = setInterval(function () {
++counter;
}, 1000);
// to stop the counter
clearInterval(myInterval);
Upvotes: 37