JavaScript--定时器案例

发布时间 2023-11-10 16:34:01作者: na2co3-
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <!-- <input type="button" onclick="on()" value="开灯"> -->
    <img id = "myImgine" border = "0" src="images/down.jpg" style="">
    <!-- <input type="button" onclick="off()" value="关灯"> -->

    <script>
        function on(){
            document.getElementById('myImgine').src='images/open.jpg';
        }
        function off(){
            document.getElementById('myImgine').src = 'images/down.jpg';
        }
    var x = 0;
    setInterval(function(){
if(x%2==0){
    on();
}
else{
    off();
}
x++;
},1000)

    </script>
</body>
</html>