javascript倒计时显示毫秒

发布于 2024-10-15 06:45:53 字数 159 浏览 4 评论 0原文

我想要倒计时,并希望显示类似分钟:秒:毫秒的格式。我用 jquery 插件 countdown 进行了倒计时,但它只显示分钟:秒格式。 有什么办法可以纠正吗? 非常感谢!

I want to do a count down and want to show like format as Minutes:Seconds:Milliseconds. I made a count down with jquery plug-in countdown but it shows just Minutes:Seconds format.
Is there any way to make it right?
Many Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

萌逼全场 2024-10-22 06:45:53

大家好,我已经为自己开发了一个代码,使用以下代码
计数器20秒

var _STOP =0;

var value=1999;

function settimer()

{
    var svalue = value.toString();

    if(svalue.length  == 3)

        svalue = '0'+svalue;
    else if(svalue.length  == 2)
        svalue = '00'+svalue;
    else if(svalue.length  == 1)
        svalue = '000'+svalue;
    else if(value == 0)
        svalue = '0000';
    document.getElementById('cn1').innerHTML = svalue[0];
    document.getElementById('cn2').innerHTML = svalue[1];
    document.getElementById('cn3').innerHTML = svalue[2];
    document.getElementById('cn4').innerHTML = svalue[3];   
    value--;

    if (_STOP==0 && value>=0) setTimeout("settimer();", 10);
}

setTimeout("settimer()", 10);

Hi guys I have developed a code for my self use the following code
counter for 20 seconds

var _STOP =0;

var value=1999;

function settimer()

{
    var svalue = value.toString();

    if(svalue.length  == 3)

        svalue = '0'+svalue;
    else if(svalue.length  == 2)
        svalue = '00'+svalue;
    else if(svalue.length  == 1)
        svalue = '000'+svalue;
    else if(value == 0)
        svalue = '0000';
    document.getElementById('cn1').innerHTML = svalue[0];
    document.getElementById('cn2').innerHTML = svalue[1];
    document.getElementById('cn3').innerHTML = svalue[2];
    document.getElementById('cn4').innerHTML = svalue[3];   
    value--;

    if (_STOP==0 && value>=0) setTimeout("settimer();", 10);
}

setTimeout("settimer()", 10);
嘿咻 2024-10-22 06:45:53

试试这个: http://jsfiddle.net/aamir/TaHtz/76/

HTML:

<div id="timer"></div>

JS:

var el = document.getElementById('timer');
var milliSecondsTime = 10000;
var timer;


el.innerHTML = milliSecondsTime/1000;
timer = setInterval(function(){
    milliSecondsTime = milliSecondsTime - 1000;
    if(milliSecondsTime/1000 == 0) {
        clearTimeout(timer);
        el.innerHTML = 'BOOOOM';
    }
    else {
        el.innerHTML = milliSecondsTime/1000;
    }
},1000);
​

Try this: http://jsfiddle.net/aamir/TaHtz/76/

HTML:

<div id="timer"></div>


JS:

var el = document.getElementById('timer');
var milliSecondsTime = 10000;
var timer;


el.innerHTML = milliSecondsTime/1000;
timer = setInterval(function(){
    milliSecondsTime = milliSecondsTime - 1000;
    if(milliSecondsTime/1000 == 0) {
        clearTimeout(timer);
        el.innerHTML = 'BOOOOM';
    }
    else {
        el.innerHTML = milliSecondsTime/1000;
    }
},1000);
​
迷路的信 2024-10-22 06:45:53

如果你想制作自己的计时器。
阅读之前的问题
如何创建 JQuery 时钟/计时器

If you want to make your own timer.
read this earlier question
How to create a JQuery Clock / Timer

你怎么敢 2024-10-22 06:45:53

尝试设置格式参数 - http://keith-wood.name/countdownRef.html#format

进一步阅读,这个插件不执行毫秒操作。此时,您要么必须编辑实际的插件代码,要么找到一个新的插件。

Try setting the format parameter - http://keith-wood.name/countdownRef.html#format

On further reading, this plugin doesn't do milliseconds. At this point, you either have to edit the actual plugin code or find a new plugin.

墨洒年华 2024-10-22 06:45:53

我完全同意@Matt Ball的评论。它也可能导致浏览器崩溃。

为什么不尝试这个解决方案

jQuery 1 分钟倒计时(以毫秒为单位)和回调

I completely agree with @Matt Ball's comment.It may also cause the browser to crash.

Why don't you try this solution instead

jQuery 1 minute countdown with milliseconds and callback

枫林﹌晚霞¤ 2024-10-22 06:45:53

我是这样做的(从 N 到 X (X > N) 的通用计数器):

var dynamicCounterAddNewValue = 20;
    var currentDynamicUpdater;

    function dynamicCounterForValueForControlUpdater(_updaterData) {
       _updaterData.from += dynamicCounterAddNewValue;

       if (_updaterData.from > _updaterData.to) {
          _updaterData.from = _updaterData.to;
       }

       _updaterData.c.html(_updaterData.from.toString());
       if (_updaterData.from < _updaterData.to) {
          currentDynamicUpdater = setTimeout(
             dynamicCounterForValueForControlUpdater,
             10,
             {
                c: _updaterData.c,
                from: _updaterData.from,
                to: _updaterData.to
             }
             );
       }
       else {
          clearTimeout(currentDynamicUpdater);
       }
       return;
    }

    // _c -> jQuery object (div,span)
    // _from -> starting number
    // _to -> ending number
    function dynamicCounterForValueForControl(_c, _from, _to) {
       clearTimeout(currentDynamicUpdater);
       dynamicCounterForValueForControlUpdater(
          {
             c: _c,
             from: _from,
             to: _to
          }
          );
       return;
    }

编辑:更新版本(更灵活 - 对于一个接一个的 N 个元素):

(输入元素是元素的数组,用于使其动态计数)

var dynamicCounterTimeout = 10;
var currentDynamicUpdater;

function odcArray(_odca) {
   this.odca = _odca;
   return;
}

function odc(_c, _from, _to) {
   this.c = _c;         // $('#control_id')
   this.from = _from;   // e.g. N
   this.to = _to;       // e.g. M => (M >= N)
   var di = parseInt(_to / 45, 10);
   if (di < 1) {
      di = 1;
   }
   this.dynamicInc = di;
   return;
}

function dynamicCounterForValueForControlUpdater(_odca) {
   if (
      _odca.odca === null
      ||
      !_odca.odca.length
      ) {
      clearTimeout(currentDynamicUpdater);
      return;
   }

   var o = _odca.odca[0];
   o.from += o.dynamicInc;

   if (o.from > o.to) {
      o.from = o.to;
      _odca.odca.shift();  // Remove first element
   }
   o.c.html(o.from.toString());

   currentDynamicUpdater = setTimeout(
      dynamicCounterForValueForControlUpdater,
      dynamicCounterTimeout,
      _odca
      );

   return;
}

function dynamicCounterForValueForControl(_odca) {
   clearTimeout(currentDynamicUpdater);
   // SETUP all counters to default
   for (var i = 0; i < _odca.odca.length; i++) {
      _odca.odca[i].c.html(_odca.odca[i].from.toString());
   }
   dynamicCounterForValueForControlUpdater(
      _odca
      );
   return;
}

I did it like this (generic counter from N to X (X > N)):

var dynamicCounterAddNewValue = 20;
    var currentDynamicUpdater;

    function dynamicCounterForValueForControlUpdater(_updaterData) {
       _updaterData.from += dynamicCounterAddNewValue;

       if (_updaterData.from > _updaterData.to) {
          _updaterData.from = _updaterData.to;
       }

       _updaterData.c.html(_updaterData.from.toString());
       if (_updaterData.from < _updaterData.to) {
          currentDynamicUpdater = setTimeout(
             dynamicCounterForValueForControlUpdater,
             10,
             {
                c: _updaterData.c,
                from: _updaterData.from,
                to: _updaterData.to
             }
             );
       }
       else {
          clearTimeout(currentDynamicUpdater);
       }
       return;
    }

    // _c -> jQuery object (div,span)
    // _from -> starting number
    // _to -> ending number
    function dynamicCounterForValueForControl(_c, _from, _to) {
       clearTimeout(currentDynamicUpdater);
       dynamicCounterForValueForControlUpdater(
          {
             c: _c,
             from: _from,
             to: _to
          }
          );
       return;
    }

EDIT: Updated version (more flexible - for N elements one after another):

(input element is Array of elements for making them dynamic-counts)

var dynamicCounterTimeout = 10;
var currentDynamicUpdater;

function odcArray(_odca) {
   this.odca = _odca;
   return;
}

function odc(_c, _from, _to) {
   this.c = _c;         // $('#control_id')
   this.from = _from;   // e.g. N
   this.to = _to;       // e.g. M => (M >= N)
   var di = parseInt(_to / 45, 10);
   if (di < 1) {
      di = 1;
   }
   this.dynamicInc = di;
   return;
}

function dynamicCounterForValueForControlUpdater(_odca) {
   if (
      _odca.odca === null
      ||
      !_odca.odca.length
      ) {
      clearTimeout(currentDynamicUpdater);
      return;
   }

   var o = _odca.odca[0];
   o.from += o.dynamicInc;

   if (o.from > o.to) {
      o.from = o.to;
      _odca.odca.shift();  // Remove first element
   }
   o.c.html(o.from.toString());

   currentDynamicUpdater = setTimeout(
      dynamicCounterForValueForControlUpdater,
      dynamicCounterTimeout,
      _odca
      );

   return;
}

function dynamicCounterForValueForControl(_odca) {
   clearTimeout(currentDynamicUpdater);
   // SETUP all counters to default
   for (var i = 0; i < _odca.odca.length; i++) {
      _odca.odca[i].c.html(_odca.odca[i].from.toString());
   }
   dynamicCounterForValueForControlUpdater(
      _odca
      );
   return;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文