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

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

报告相同问题?

悬赏问题

  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误