利用递归函数实现setTimeout模拟setInterval效果

需求:

页面每隔1s输出当前时间

输出当前时间可以使用:new Date().toLocaleString()

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <div></div>
    <script>
        function getTime(){
            document.querySelector('div').innerHTML=new Date().toLocaleString()
            setTimeout(getTime,1000)
        }
        getTime()
    </script>
</body>
</html>

代码很简单,主要强调递归的思想

猜你喜欢

转载自blog.csdn.net/L19541216/article/details/130084133
今日推荐