【问题标题】:countdown timer not working properly for seconds倒数计时器无法正常工作几秒钟
【发布时间】:2017-11-28 23:56:43
【问题描述】:

我正在为竞价拍卖创建一个倒数计时器,对于单个产品它正在工作,但是当我为所有产品设置为动态时,它显示计时器但秒数没有改变,虽然计时器工作正常,但计时器没有如您在此链接中看到的那样在前端运行http://proffice.in/bidapp/ 请帮助我解决javascript错误。显示 I 控制台的错误:

未捕获的类型错误:无法在此行上设置属性 'innerHTML' 为 null document.getElementById("cntdwn_"+show_id).innerHTML = DisplayStr; 在 countdown.js 上

    <?php 
        while($pro_data = $db->jas_fetch_array($products)){ 

             $closedate = date_format(date_create($pro_data['selling_end_date']), 'm/d/Y H:i:s');?>

                                        <script>
                                        TargetDate = "<?php echo $closedate ?>";
                                        ForeColor = "#04BFBF";
                                        show_id="<?=$pro_data["id"]?>";
                                        CountActive = true;
                                        CountStepper = -1;
                                        LeadingZero = true;
                                        DisplayFormat = "%%D%%d:%%H%%H:%%M%%M:%%S%%S";
                                        FinishMessage = "Bidding closed!";
                                        </script>
                                        <script language="JavaScript" src="<?=$wwwroot;?>assets/js/countdown.js"></script>
                                        <?php } ?>

    // here is my coutdown.js file    

    function calcage(secs, num1, num2) {
      s = ((Math.floor(secs/num1))%num2).toString();
      if (LeadingZero && s.length < 2)
        s = "0" + s;
      return "<b>" + s + "</b>";
    }

    function CountBack(secs,show_id) {
      if (secs < 0) {
        document.getElementById("cntdwn_"+show_id).innerHTML = FinishMessage;
        return;
      }
      DisplayStr = DisplayFormat.replace(/%%D%%/g, calcage(secs,86400,100000));
      DisplayStr = DisplayStr.replace(/%%H%%/g, calcage(secs,3600,24));
      DisplayStr = DisplayStr.replace(/%%M%%/g, calcage(secs,60,60));
      DisplayStr = DisplayStr.replace(/%%S%%/g, calcage(secs,1,60));

      document.getElementById("cntdwn_"+show_id).innerHTML = DisplayStr;
      if (CountActive)
        setTimeout("CountBack(" + (secs+CountStepper) + ")", SetTimeOutPeriod);
    }

    function putspan(backcolor, forecolor,show_id) {
     document.write("<span id='cntdwn_"+show_id+"' style='background-color:" + backcolor + 
                    "; color:" + forecolor + "'></span>");
    }

    if (typeof(BackColor)=="undefined")
      BackColor = "white";
    if (typeof(ForeColor)=="undefined")
      ForeColor= "black";
    if (typeof(show_id)=="undefined")
      show_id = "";
    if (typeof(TargetDate)=="undefined")
      TargetDate = "21/06/2017 5:00 AM";
    if (typeof(DisplayFormat)=="undefined")
      DisplayFormat = "%%D%% Days, %%H%% Hours, %%M%% Minutes, %%S%% Seconds.";
    if (typeof(CountActive)=="undefined")
      CountActive = true;
    if (typeof(FinishMessage)=="undefined")
      FinishMessage = "";
    if (typeof(CountStepper)!="number")
      CountStepper = -1;
    if (typeof(LeadingZero)=="undefined")
      LeadingZero = true;


    CountStepper = Math.ceil(CountStepper);
    if (CountStepper == 0)
      CountActive = false;
    var SetTimeOutPeriod = (Math.abs(CountStepper)-1)*1000 + 990;
    putspan(BackColor, ForeColor,show_id);
    var dthen = new Date(TargetDate);
    var dnow = new Date();
    if(CountStepper>0)
      ddiff = new Date(dnow-dthen);
    else
      ddiff = new Date(dthen-dnow);
    gsecs = Math.floor(ddiff.valueOf()/1000);
    CountBack(gsecs,show_id);


//  console is showing error in this line

    document.getElementById("cntdwn_"+show_id).innerHTML = DisplayStr;
      if (CountActive)
        setTimeout("CountBack(" + (secs+CountStepper) + ")", SetTimeOutPeriod);
    }

【问题讨论】:

    标签: javascript


    【解决方案1】:

    您正在做的是您只是使用循环创建脚本。但不要在需要显示计数器的地方创建 Div,这当然是你的计数器。

    请检查是否存在id为“cntdwn_”且对应show_id的div。在您的网址中,我无法在页面上找到。希望能帮助到你。如果没有,请告诉我。 :)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-11-24
      相关资源
      最近更新 更多