Javascript 简单的 jquery 秒计数器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3049553/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 03:05:19  来源:igfitidea点击:

simple jquery second counter

javascriptjquerycount

提问by Kevin Brown

What is the simplest way to increase a variable by 1 every second?

每秒将变量增加 1 的最简单方法是什么?

回答by g.d.d.c

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);

回答by Gert Grenander

The simplest way is setInterval("x++",1000);, where x++can be replaced with your increment. Example:

最简单的方法是setInterval("x++",1000);, wherex++可以用你的增量替换。例子:

JavaScript/jQuery

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

HTML

<a href="#" id="showval">Show value</a>

回答by born2net

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

回答by Zmorajlb

function timer(seconds, element)
author: ZMORA JLB
email: zmorajlb[monkey]gmail.com

函数计时器(秒,元素)
作者:ZMORA JLB
电子邮件: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 --:--

秒:计算“3605”秒--:--