简体   繁体   English

试图让多个计时器在单页上工作

[英]Trying to get multiple timers to work on single page

I've got a single page that I need a timer to show up again and again on as the user continues to read further down the page. 我有一个页面,我需要一个计时器一次又一次地显示,因为用户继续向下阅读页面。

It's the same timer, it just counts down for a set period of time. 它是相同的计时器,只是在一段时间内倒计时。

I've got the timer working perfectly in the first content span only, however it won't display in all the other elements I've specified. 我已经让计时器在第一个内容范围内完美运行,但它不会显示在我指定的所有其他元素中。

Here's what the code looks like from within the .js file: 以下是.js文件中的代码:

 document.getElementById('minutes').innerHTML = padNums(minutes, 2);
 document.getElementById('seconds').innerHTML = padNums(seconds, 2);
 document.getElementById('milliseconds').innerHTML = padNums(milliseconds, 3);   

The script from within the HTML file: HTML文件中的脚本:

 $('.counter').html('<span id="minutes">0</span>' + ':' + '<span id="seconds">0</span>' + ':' + '<span id="milliseconds">0</span>' );  

And the HTML: 和HTML:

  <div class="wrapper">
    <p> There is this much time left: <span class="counter"> </span> </p>
    <p> There is this much time left: <span class="counter"> </span> </p> 
    <p> There is this much time left: <span class="counter"> </span> </p> 
  </div>  

.......... ..........

I've tried changing the .js methods to getElementsByClass and changing the spans to classes instead of id's. 我已经尝试将.js方法更改为getElementsByClass并将跨度更改为类而不是id。 and that stopped even the first countdown from working. 这甚至阻止了第一次倒计时工作。

What else can I try? 我还能尝试什么?

Thanks 谢谢


Here's another set of code I've tried as suggested from below: 这是我尝试过的另一组代码,如下所示:

Inside the html page: 在html页面内:

 <div class="wrapper">
    <p> There is this much time left: <span class="counter"> </span> </p>
    <p> There is this much time left: <span class="counter"> </span> </p> 
    <p> There is this much time left: <span class="counter"> </span> </p> 
  </div>

  <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script> 
  <script type="text/javascript">

  $('.counter').html('<span class="minutes">0</span>' + ':' + '<span     
  class="seconds">0</span>' + ':' + '<span class="milliseconds">0</span>' );


  </script>    

And within the .js: 在.js内:

 $('.minutes').innerHTML = padNums(minutes, 2);
 $('.seconds').innerHTML = padNums(seconds, 2);
 $('.milliseconds').innerHTML = padNums(milliseconds, 3); 

Although... this doesn't work either. 虽然......这也不起作用。


The easiest way for me to accomplish this would be to create more sets of unique ids for each timer but I want a more elegant way of doing this. 对我来说,最简单的方法是为每个计时器创建更多的唯一ID,但我想要一种更优雅的方法。

Try using classes for spans #minutes, #seconds and #milliseconds instead of IDs. 尝试使用类的跨度#minutes,#else和#milliseconds而不是ID。

You will then have to replace the three 然后你必须更换这三个

document.getElementById('minutes') ... 

with

$('.minutes') ...

Just saw your edit. 刚看到你的编辑。

You will need to use 你需要使用

$('.minutes').html('...')

in the same way you did with 和你一样

$('.counter').html('...')

For php I made this class to handle situations like that: 对于php,我让这个类来处理这样的情况:

class IDDateTime {

    static function SecToTime($sec) {
        $days = floor($sec / 86400);
            $sec = $sec - ($days * 86400);
        $hours = floor($sec / 3600);
            $sec = $sec - ($hours * 3600);
        $minutes = floor($sec / 60);
            $sec = $sec - ($minutes * 60);
        $seconds = $sec;

        if($days > 0) $days= $days.' days'; else $days='';
        if ($minutes < 10) $minutes= '0'.$minutes;
        if ($seconds < 10) $seconds= '0'.$seconds;

        return $days.$hours.':'.$minutes.':'.$seconds;
    }

    static function ShowTimer($id,$sec,$href='',$extrasec=false) {
        return '<span id="'.$id.'">'.IDDateTime::SecToTime($sec).'</span><script type="text/javascript">updateTime("'.$id.'",'.($sec + ($extrasec?1:0)).',"'.$href.'");</script>';
    }

    static function AddTimerEngine($text='Done') {
        return '
        <script type="text/javascript">
        function updateTime(elementId,time,rdr) {
            var bt_obj = document.getElementById(elementId); 
            time--;
            var timeToEnd = time; 
            temp = timeToEnd;
            if(temp >= 0) { 
                days = Math.floor(temp / 86400);
                temp = temp % 86400;
                hours = Math.floor(temp / 3600);
                temp = temp % 3600; minutes = Math.floor(temp / 60);
                temp = temp % 60; output = "";
                seconds = temp;                
                if(days > 0) output = days + "days. "; 
                if(seconds < 10) seconds = "0" + seconds; 
                if(minutes < 10) minutes = "0" + minutes; 
                bt_obj.innerHTML = "<b>"+output+hours+":"+minutes+":"+seconds+"</b>"; 
                var jsExp = "updateTime(\'" + elementId + "\'," + time +",\'"+ rdr + "\')";
                setTimeout(jsExp , 1000); 
            } else {
                bt_obj.innerHTML = "'.$text.'";
                if(rdr != \'\') {
                    window.location.href = rdr;
                }
            }
        };
        </script>';
    }

}

To use it just add it to your page like that: 要使用它,只需将其添加到您的页面:

Echo IDDateTime::ShowTimer('yout_timer_id',$seconds);
Echo IDDateTime::ShowTimer('another_timer',$seconds);

You can take some parts of the code and adapt to your situation, or use it as is. 您可以获取代码的某些部分并适应您的情况,或按原样使用它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM