定时器 延时调用setTimeout

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>定时器 延时调用setTimeout</title>
    <!-- 
        setTimeout("代码a",n);打开页面n毫秒后执行代码a
        clearTimeout(setTimeout());取消setTimeout();
     -->
</head>
<body>
    <script>
        var x;//全局变量x
        function a(){
            x=setTimeout("document.write('我在5秒后显示')",1000);//将定时代码赋值给x
        }
        function b(){
            clearTimeout(x);//清除x
        }
        a();//调用a
        b();//调用b
    </script>
</body>
</html>

猜你喜欢

转载自www.cnblogs.com/vinson-blog/p/12077403.html