JavaScript使用计时事件制作的电子时钟

本例出自:菜鸟教程


<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>使用计时事件制作的电子时钟title>
<script>
    function startTime() {
        var now = new Date();
        var h = now.getHours();
        var m = now.getMinutes();
        var s = now.getSeconds();

        m = checkTime(m);
        s = checkTime(s);

        var nowStr = h + ":" + m + ":" + s;
        document.getElementById("demo").innerHTML = nowStr;

        //设置每0.5秒执行一次函数startTime()
        setTimeout("startTime()", 500)
    }

    //为了美观,补足分秒前面的0
    function checkTime(x) {
        if (x < 10) {
            return "0" + x;
        }
        return x;
    }
script>
head>
<body onload="startTime()">
<div id="demo">div>
body>
html>

你可能感兴趣的:(JavaScript)