Get the App
SLTechnology News&Howtos  ›  Development  › 

How to realize the traffic light in javascript

Shulou Source: shulou.com Published: 2022-06-02 14:49:49 09月18日 Update

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!

Tags: Traffic lights colors loops recursion code content methods methods more knowledge practicality a series of learning success and then dilemmas realities situations effects tutorials articles Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MySQL Linux vpn Shulou Tech Info Redmi