倒数计时器在几秒钟内无法正常工作

时间:2017-06-26 07:18:36

标签: javascript

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

  

未捕获的TypeError:无法在此行上设置null的“innerHTML”属性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);
    }

1 个答案:

答案 0 :(得分:0)

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

请检查是否有任何ID为id&#34; cntdwn _&#34;与您对应的show_id是否存在。在您的网址中,我无法在页面上找到。希望能帮助到你。如果它没有,请告诉我。 :)