2010-06-15 3 views

답변

34
var counter = 0; 
setInterval(function() { 
    ++counter; 
}, 1000); 

는 또한,이 수 가능 그 :

var counter = 0; 
var myInterval = setInterval(function() { 
    ++counter; 
}, 1000); 

// to stop the counter 
clearInterval(myInterval); 
+0

@ShintoJoseph - 당신은이 DOM에 자신을 추가 믿을 수 없어 당신은 의미합니까? 그것은 질문이 아니었고,이 대답은 거의 5 년간 받아 들여졌습니다. –

+0

네, 고마워요. –

0

가장 간단한 방법은 x++이 당신의 증가로 대체 할 수 setInterval("x++",1000);있다. 예 :

자바 스크립트/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> 
+0

'setInterval ("x ++", 1000);'은 묵시적인 eval()을 사용합니다. 대부분의 경우 문제는 아니지만 기술적으로 보안 위험이 아닙니까? –

+0

그렇다면 왜 비활성화하지 않았습니까? 여전히 변수를 늘리는 가장 간단한 방법입니다. –

0

더 좋은 방법은 폐 기능을 통해입니다 :

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); 
    }, 
+0

왜 이것이 허용 된 답변보다 나은지 설명해 주시겠습니까? – AXL

0

기능 타이머 (세코 NDS 요소)
저자 : ZMORA JLB
이메일 : zmorajlb [원숭이] gmail.com

충전 기능 타이머 포함 :

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,{})) 

사용

$(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> 

초 : 세는 "3605"초 - : -