JS 中使用Promise 實(shí)現(xiàn)紅綠燈實(shí)例代碼(demo)
要求
- 使用promise 實(shí)現(xiàn)紅綠燈顏色的跳轉(zhuǎn)
- 綠燈執(zhí)行三秒后
- 黃燈執(zhí)行四秒后
- 紅燈執(zhí)行五秒
html 實(shí)現(xiàn)如下:
<ul id="traffic" class=""> <li id="green"></li> <li id="yellow"></li> <li id="red"></li> </ul>
定義一個空類,之后再js中操作對應(yīng)的類名即可實(shí)現(xiàn)相關(guān)的效果。
CSS實(shí)現(xiàn)如下:
ul {
position: absolute;
width: 200px;
height: 200px;
top: 50%;
left: 50%;
transform: translate(-50%,-50%);
}
/*畫3個圓代表紅綠燈*/
ul >li {
width: 40px;
height: 40px;
border-radius:50%;
opacity: 0.2;
display: inline-block;
}
/*執(zhí)行時改變透明度*/
ul.red >#red,
ul.green >#green,
ul.yellow >#yellow{
opacity: 1.0;
}
/*紅綠燈的三個顏色*/
#red {background: red;}
#yellow {background: yellow;}
#green {background: green;}
javascript原理:
promise函數(shù)為一個異步操作函數(shù),在函數(shù)運(yùn)行結(jié)束時可以使用then()方法。我們再在promise函數(shù)內(nèi)部設(shè)置延遲執(zhí)行即可實(shí)現(xiàn)。
js 代碼如下:
function timeout(timer){
return function(){
return new Promise(function(resolve,reject){
setTimeout(resolve,timer)
})
}
}
var green = timeout(3000);
var yellow = timeout(4000);
var red = timeout(5000);
var traffic = document.getElementById("traffic");
(function restart(){
'use strict' //嚴(yán)格模式
console.log("綠燈"+new Date().getSeconds()) //綠燈執(zhí)行三秒
traffic.className = 'green';
green()
.then(function(){
console.log("黃燈"+new Date().getSeconds()) //黃燈執(zhí)行四秒
traffic.className = 'yellow';
return yellow();
})
.then(function(){
console.log("紅燈"+new Date().getSeconds()) //紅燈執(zhí)行五秒
traffic.className = 'red';
return red();
}).then(function(){
restart()
})
})();
ps:下面看一個Promise用法例子
注意:要想then方法能鏈?zhǔn)降膱?zhí)行下去,必須返回Promise對象!??!
'use strict';
function async(value) {
return new Promise(function(resolve, reject) {
var ms = Math.round(Math.random() * 1000);
setTimeout(function() {
console.log('waiting ' + ms + 'ms');
// 等待ms毫秒
resolve(value + ms);
}, ms);
});
}
// 每次執(zhí)行隨機(jī)等待n毫秒,結(jié)果統(tǒng)計(jì)總毫秒數(shù)
async(0)
.then(async)
.then(async)
.then(async)
.then(async)
.then(function(value) {
console.log('------total wait:' + value + 'ms');
});
////////////////////////////////////////////////////////////
function async1(value) {
return new Promise(function(resolve, reject) {
resolve(value + 1);
});
}
function async2(value) {
// return new Promise(function(resolve, reject) {
// resolve(value + 2);
// });
// 等價(jià)與上面寫法
return Promise.resolve(value + 2);
}
function async3(value) {
return new Promise(function(resolve, reject) {
resolve(value + 3);
});
}
async1(100).then(async2).then(async3).then(function(value) {
console.log('------' + value);
});
/////////////////////////////////////////////////////////////////
function say() {
var value = 'what';
return Promise.resolve(value);
}
say().then(function(value) {
value = value + ' are';
return Promise.resolve(value);
}).then(function(value) {
value = value + ' you';
return Promise.resolve(value);
}).then(function(value) {
value = value + ' doing';
return Promise.resolve(value);
//return Promise.reject('error, exit'); // 中途退出
}).then(function(value) {
value = value + ' now!';
return Promise.resolve(value);
}).then(function(value) {
console.log('------' + value);
}).catch(function(error) {
console.log('------' + error);
});
<html>
<head>
<title>Ball move</title>
<style type="text/css">
.ball {
width: 40px;
height: 40px;
border-radius: 20px;
margin-left: 10px;
}
.ball1 {
background: #ff0000;
}
.ball2 {
background: #00ff00;
}
.ball3 {
background: #0000ff;
}
</style>
<script src="http://libs.baidu.com/jquery/1.9.0/jquery.js"></script>
</head>
<body>
<div class="ball ball1"></div>
<div class="ball ball2"></div>
<div class="ball ball3"></div>
<script type="text/javascript">
function moving(ball, pos) {
return new Promise(function(resolve, reject) {
var marginLeft = parseInt(ball.css('margin-left'));
if (marginLeft != pos) {
var timerId = setInterval(function() {
marginLeft = marginLeft + 1;
ball.css('margin-left', marginLeft);
if (marginLeft == pos) {
clearInterval(timerId);
resolve();
}
}, 20);
} else {
resolve();
}
});
}
moving($('.ball1'), 100).then(function() {
return moving($('.ball2'), 150);
}).then(function() {
return moving($('.ball3'), 200);
});
</script>
</body>
</html>
總結(jié)
以上所述是小編給大家介紹的JS 中使用Promise 實(shí)現(xiàn)紅綠燈實(shí)例代碼(demo),希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
相關(guān)文章
JS實(shí)現(xiàn)動態(tài)增加和刪除li標(biāo)簽行的實(shí)例代碼
下面小編就為大家?guī)硪黄狫S實(shí)現(xiàn)動態(tài)增加和刪除li標(biāo)簽行的實(shí)例代碼。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2016-10-10
JavaScript 保存數(shù)組到Cookie的代碼
大部分的瀏覽器一個網(wǎng)站只支持保存20個Cookie,超過20個Cookie,舊的Cookie會被最新的Cookie代替。那么如果要有超過20個Cookie要保存只能將Cookie存為數(shù)組然后保存到Cookie。2010-04-04
Js實(shí)現(xiàn)中國公民身份證號碼有效性驗(yàn)證實(shí)例代碼
這篇文章主要介紹了Js實(shí)現(xiàn)中國公民身份證號碼有效性驗(yàn)證實(shí)例代碼,可以識別身份證號碼的正確性,有興趣的可以了解一下2017-05-05
JS數(shù)組進(jìn)階示例【數(shù)組的幾種函數(shù)用法】
這篇文章主要介紹了JS數(shù)組進(jìn)階,結(jié)合實(shí)例形式總結(jié)分析了數(shù)組的幾種常見函數(shù)基本用法,涉及JavaScript數(shù)組元素刪除、拼接、添加、倒序排列等相關(guān)操作技巧,需要的朋友可以參考下2020-01-01
javascript中JSON對象與JSON字符串相互轉(zhuǎn)換實(shí)例
這篇文章主要介紹了javascript中JSON對象與JSON字符串相互轉(zhuǎn)換,實(shí)例分析了json對象與字符串常用的幾種轉(zhuǎn)換技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-07-07
JavaScript仿淘寶實(shí)現(xiàn)固定右側(cè)側(cè)邊欄
這篇文章主要為大家詳細(xì)介紹了如何利用JavaScript實(shí)現(xiàn)仿淘寶固定右側(cè)側(cè)邊欄效果,文中示例代碼介紹的非常詳細(xì),感興趣的小伙伴們可以參考一下2022-03-03

