使用JavaScript实现红绿灯切换

使用JavaScript实现红绿灯切换

这个方法不建议用,es6已经不允许使用callee了,只是当练习了解一下callee

<!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>Document</title>
</head>
<body>
    <script>
			function getLight(first,second,third){
			            first(second,third);
			        }
			
			        function getRedLight(fn,fn1){
			           var f=arguments.callee;
			            var ids=setTimeout(function(){
			                console.log("红灯");
			                clearTimeout(ids);
			                fn(fn1,f);
			            },1000);
			        }
			        function getYellowLight(fn,fn1){
			            var f=arguments.callee;
			            var ids=setTimeout(function(){
			                console.log("黄灯");
			                clearTimeout(ids);
			                fn(fn1,f);
			            },1000);
			        }
			        function getGreenLight(fn,fn1){
			            var f=arguments.callee;
			            var ids=setTimeout(function(){
			                console.log("绿灯");
			                clearTimeout(ids);
			                fn(fn1,f);
			            },1000);
			        }
			
			        getLight(getGreenLight,getYellowLight,getRedLight);
            </script>
</body>
</html>

实现效果如下,在控制台可以每秒打印不同的灯
控制台打印详情

发布了8 篇原创文章 · 获赞 0 · 访问量 142

猜你喜欢

转载自blog.csdn.net/weixin_44157964/article/details/103759231