dongqufi82315 2015-09-15 06:07
浏览 73
已采纳

多个倒计时器不在单页上工作

i have multiple timers on a page but the code doesn't seem to work for it.

in the code below the timer for each post gets started with onload function which calls the jquery code

the code i used i got it from another thread here i just modified it to work with onload for each span but it doesn't work then

 <?php
$i=0;
$timeleft="05:00:30";
while ( $loop->have_posts() ) : $loop->the_post();
?>
<span id="countdown<?php echo $i; ?>" onload="start_timer(<?php echo $i; ?>,<?php echo $timeleft; ?>)"> <?php echo $timeleft; ?> </span>    
<?php $i++;endwhile;wp_reset_postdata(); ?>

<script type="text/javascript">
function start_timer(num,time){
  $(document).ready(function() {  
      parts = time.split(':'),
      hours = +parts[0],
      minutes = +parts[1],
      seconds = +parts[2],
      span = $('#countdown'+num);

    function correctNum(num) {
      return (num<10)? ("0"+num):num;
    }

    var timer = setInterval(function(){
        seconds--;
        if(seconds == -1) {
            seconds = 59;
            minutes--;

            if(minutes == -1) {
                minutes = 59;
                hours--;

                if(hours==-1) {
                  alert("timer finished");
                  clearInterval(timer);
                  return;
                }
            }
        }
        span.text(correctNum(hours) + ":" + correctNum(minutes) + ":" + correctNum(seconds));
    }, 1000);
  }); 
}
</script>
  • 写回答

1条回答 默认 最新

  • dtstnjl898781429 2015-09-15 06:56
    关注

    onload is only supported on the following elements

    you can do something like this:

    <?php
    while ( $loop->have_posts() ) : $loop->the_post();
    ?>
    <span id="countdown<?php echo $i; ?>"> <?php echo $timeleft; ?> </span>
    <script>start_timer(<?php echo $i; ?>,'<?php echo $timeleft; ?>');</script>
    <?php $i++;endwhile;wp_reset_postdata(); ?>
    

    Edit:

    change you javascript to this:

    <script type="text/javascript">
    function myFormat(num){
        return (num < 10)? ("0"+num): num;
    }
    function StartTimer(num, time){
        this.parts = time.split(':');
        this.hours = this.parts[0];
        this.minutes = this.parts[1];
        this.seconds = this.parts[2];
        this.span = $('#countdown' + num);
    }
    StartTimer.prototype.myTimer = function(){
        var s = this.seconds;
        var m = this.minutes;
        var h = this.hours;
        var sp = this.span;
        var t = setInterval(function(){
            s--;
            if(s == -1) {
                s = 59;
                m--;
                if(m == -1) {
                    m = 59;
                    h--;
                    if(h == -1) {
                        alert("timer finished");
                        clearInterval(t);
                        return;
                    }
                }
            }
            //console.log();
            //console.log();
            //console.log();
            sp.text(h + ":" + myFormat(m) + ":" + myFormat(s));
        }, 1000);
    };
    </script>
    

    and your while loop:

    <?php
    while ( $loop->have_posts() ) : $loop->the_post();
    ?>
    <span id="countdown<?php echo $i; ?>"> <?php echo $timeleft; ?> </span>
    <script>new StartTimer(<?php echo $i; ?>,'<?php echo $timeleft; ?>').myTimer();</script>
    <?php $i++;endwhile;wp_reset_postdata(); ?>
    

    you can also check it in JSFiddle here

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 latex怎么处理论文引理引用参考文献
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?