不显示当前时间

时间:2016-06-02 11:18:44

标签: javascript html time

 <script>
function updateClock() {
    var now = new Date();
    var time = now.getHours() + ':' + now.getMinutes() + ':' + now.getSeconds(); 
    document.getElementById('current').innerHTML = time;
    setTimeout(updateClock, 1000);
}
function check()
{
    var now= new Date();
    var h=document.getElementById('hour').value;
    var m=document.getElementById('minutes').value;
    if((now.getHours()==h)&&(now.getMinutes()==m))
        document.getElementById('display').innerHTML="Alarm tone is playing";
    setTimeout(check, 1000);
}
function setAlarm(){
    var msg="Alarm is set for ";
    var ha=Number(document.getElementById('hour').value);
    var ma=Number(document.getElementById('minutes').value);
    var now=new Date();
    var hc=now.getHours();
    var mc=now.getMinutes();
    var nh=0,nm=0;
    if(hc>ha){
        nh=24-hc;
        //document.getElementById('display').innerHTML=nh;
        nh=nh+ha;
        if(mc>ma){
            nh--;
            nm=60-mc;
            nm=nm+ma;
        }
        else
            nm=ma-mc;
        msg=msg+nh+" hours "+nm+" minutes from now ";
        document.getElementById('alarmmsg').innerHTML=msg;
    }
    /*else if(hc==ha){
        if(mc>ma){
            nh=24-hc;
            nh=nh+ha;
            if(mc>ma){
                nh--;
                nm=60-mc;
                nm=nm+ma;
            }
            else
                nm=ma-mc;
            }
        }
        else{
            nh=0;
            nm=ma-mc;
        }
    }
    else{
        nh=ha-hc;
        if(mc>ma){
            nh--;
            nm=60-mc;
            nm=nm+ma;
        }
        else{
            nm=ma-mc;
        }
    }
    msg=msg+nh+" hours "+nm+" minutes from now";
    setTimeout(function(){
         document.getElementById("alarm").innerHTML=msg;
         },3000);*/
    check();

}
</script>

以上是显示当前时间,设置闹钟以及计算和显示闹钟剩余时间(闹钟信息)的代码。

从上面的代码中删除一些评论,这里是更新后的代码fiddle

它不显示当前时间以及警报信息。

是因为多次使用setTimeout()还是其他原因。非常感谢帮助。

谢谢。

1 个答案:

答案 0 :(得分:0)

你在那个庞大的IF声明中有一些缺失的括号:

这两行具体。

else
 nm=ma-mc;

以下是经过纠正且功能正常的大型IF

if(hc>ha)
  { nh=24-hc;
    //document.getElementById('display').innerHTML=nh;
    nh=nh+ha;
    if(mc>ma)
     {  nh--;
        nm=60-mc;
        nm=nm+ma;
     }
    else
     {  nm=ma-mc;
    //msg=msg+nh+" hours "+nm+" minutes from now ";
    //document.getElementById('alarmmsg').innerHTML=msg;
     }
  }
else if(hc==ha)
  { if(mc>ma)
      { nh=24-hc;
        nh=nh+ha;
        if(mc>ma)
         {  nh--;
            nm=60-mc;
            nm=nm+ma;
         }
        else
         { nm=ma-mc;
         }
      }
    else
     {  nh=0;
        nm=ma-mc;
     }
  }
else
  { nh=ha-hc;
    if(mc>ma)
     {  nh--;
        nm=60-mc;
        nm=nm+ma;
     }
    else
     { nm=ma-mc;
     }
  }
相关问题