Using Promise in JS to implement traffic light example code (demo)
This article introduces you to the use of Promise in JS to achieve traffic light effects through example code. It also introduces an example of promise usage. Friends who do not know how to use Promise in JS can refer to this article
Requirements
Use promise to realize the jump of traffic light color
The green light is executed for three seconds After
After the yellow light is executed for four seconds
The red light is executed for five seconds
html implementation As follows:
1 2 3 4 5 |
|
Define an empty class, and then operate the corresponding class name in js to achieve related effects.
CSS is implemented as follows:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
|
javascriptPrinciple:
The promise function is an asynchronous operation function that ends after the function runs You can use the then() method. We can achieve this by setting delayed execution inside the promise function.
js code is as follows:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
|
ps: Let’s look at an example of Promise usage
Note: If you want the then method to If the chain execution continues, the Promise object must be returned! ! !
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 |
|
The above is the example code (demo) of using Promise to implement traffic lights in JS introduced by the editor. I hope it will be helpful to everyone! !
Related recommendations:
The specific method of using Promise in jQuery
How to implement a complete Promise
About the simple usage of promise objects
The above is the detailed content of Using Promise in JS to implement traffic light example code (demo). For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

1. Users who want to use the traffic light navigation function of Amap must first update the Amap map to the latest version, and then click Driving on the homepage (as shown in the picture). 2. Then enter the starting point and destination, and click to start navigation (as shown in the picture). 3. Finally, when you are about to pass the traffic light on the map, you can see the countdown display (as shown in the picture).

How to use WebSocket and JavaScript to implement an online speech recognition system Introduction: With the continuous development of technology, speech recognition technology has become an important part of the field of artificial intelligence. The online speech recognition system based on WebSocket and JavaScript has the characteristics of low latency, real-time and cross-platform, and has become a widely used solution. This article will introduce how to use WebSocket and JavaScript to implement an online speech recognition system.

Amap can set the traffic light countdown, so how to set this function specifically? Users need to open more settings in Amap and then find the custom navigation function to count seconds while navigating. This traffic light countdown setting method tutorial will tell you how to set it up, come and take a look! Amap usage tutorial: How to set the traffic light countdown in Amap navigation 1. First open the software, enter the navigation, and then click More on the lower right. 2. Click the custom navigation button inside. 3. There is an electronic eye display function below, which can be opened by clicking on it.

WebSocket and JavaScript: Key technologies for realizing real-time monitoring systems Introduction: With the rapid development of Internet technology, real-time monitoring systems have been widely used in various fields. One of the key technologies to achieve real-time monitoring is the combination of WebSocket and JavaScript. This article will introduce the application of WebSocket and JavaScript in real-time monitoring systems, give code examples, and explain their implementation principles in detail. 1. WebSocket technology

Introduction to how to use JavaScript and WebSocket to implement a real-time online ordering system: With the popularity of the Internet and the advancement of technology, more and more restaurants have begun to provide online ordering services. In order to implement a real-time online ordering system, we can use JavaScript and WebSocket technology. WebSocket is a full-duplex communication protocol based on the TCP protocol, which can realize real-time two-way communication between the client and the server. In the real-time online ordering system, when the user selects dishes and places an order

How to use WebSocket and JavaScript to implement an online reservation system. In today's digital era, more and more businesses and services need to provide online reservation functions. It is crucial to implement an efficient and real-time online reservation system. This article will introduce how to use WebSocket and JavaScript to implement an online reservation system, and provide specific code examples. 1. What is WebSocket? WebSocket is a full-duplex method on a single TCP connection.

In daily life, we often encounter problems between promises and fulfillment. Whether in a personal relationship or a business transaction, delivering on promises is key to building trust. However, the pros and cons of commitment are often controversial. This article will explore the pros and cons of commitments and give some advice on how to keep your word. The promised benefits are obvious. First, commitment builds trust. When a person keeps his word, he makes others believe that he is a trustworthy person. Trust is the bond established between people, which can make people more

JavaScript and WebSocket: Building an efficient real-time weather forecast system Introduction: Today, the accuracy of weather forecasts is of great significance to daily life and decision-making. As technology develops, we can provide more accurate and reliable weather forecasts by obtaining weather data in real time. In this article, we will learn how to use JavaScript and WebSocket technology to build an efficient real-time weather forecast system. This article will demonstrate the implementation process through specific code examples. We
