Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize the traffic light in javascript

2025-04-08 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article introduces the relevant knowledge of "how to achieve traffic lights in javascript". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Javascript to achieve traffic light methods: 1, use setTimeout and recursion to achieve cyclic color change; 2, use Promise, and write the next color change in then; 3, use async await and while to achieve traffic light effect.

The operating environment of this tutorial: windows7 system, javascript1.8.5 version, Dell G3 computer.

How does javascript realize the traffic light?

Realization of traffic lights by JavaScript

   uses setTimeout, Promise, async await three ways to achieve traffic light code, red light 2 seconds, yellow light 1 second, green light 3 seconds, cycle to change color. The way to change the color is simply to print the color.

SetTimeout implementation

   using setTimeout is the most basic implementation, the code is as follows, using recursion to achieve loop color change.

Function changeColor (color) {console.log ('traffic-light', color);} function main () {changeColor ('red'); setTimeout () = > {changeColor (' yellow'); setTimeout () = > {changeColor ('green'); setTimeout (main, 2000);}, 1000);}, 2000);} main ()

Promise implementation

   uses Promise, writes the next color change in then, and finally uses recursion to complete the loop.

Function sleep (duration) {return new Promise (resolve = > {setTimeout (resolve, duration);})} function changeColor (duration,color) {return new Promise (resolve = > {console.log ('traffic-light', color); sleep (duration) .then (resolve)) })} function main () {return new Promise (resolve = > {changeColor (2000, 'red'). Then () = > {changeColor (1000,' yellow'). Then () = > {changeColor (3000, 'green'). Then () = > {main ();})} main ()

Async await implementation

   uses async await to avoid the chain of .then.then of Promise, eliminates the need for recursion, and uses while to implement loops.

Function sleep (duration) {return new Promise (resolve = > {setTimeout (resolve, duration);})} async function changeColor (color, duration) {console.log ('traffic-light', color); await sleep (duration);} async function main () {while (true) {await changeColor ('red', 2000); await changeColor (' yellow', 1000); await changeColor ('green', 3000);} main (); "how javascript implements traffic lights" is here. Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report