web蓝桥杯真题:灯的颜色变化

代码及注释:

javascript 复制代码
// TODO:完善此函数 显示红色颜色的灯
function red() {    //将红色图片元素display显示出来,其他隐藏
    document.querySelector('#defaultlight').style.display = 'none'
    document.querySelector('#redlight').style.display = 'inline-block'
    document.querySelector('#greenlight').style.display = 'none'
}

// TODO:完善此函数  显示绿色颜色的灯
function green() {
    document.querySelector('#defaultlight').style.display = 'none'
    document.querySelector('#redlight').style.display = 'none'
    document.querySelector('#greenlight').style.display = 'inline-block'
}

// TODO:完善此函数
function trafficlights() {
    setTimeout(red(), 3000)    //3秒后执行变红函数
    setTimeout(green(), 6000)
}

trafficlights();

知识点:

1.延时器 setTimeout()

指定毫秒数之后调用函数,只执行一次

2.定时器 setInterval()

指定毫秒数为周期不断调用函数,直到clearInterval()被调用或窗口关闭,setInterval()会返回一个id值可作为clearInterval()的参数

相关推荐
鹿野素材屋6 分钟前
技术闲聊:为什么网游会在固定时间点,刷出固定的道具?
前端·网络·unity
同学807969 分钟前
🔥🔥高效易用的 Vue3 公告滚动组件:打造丝滑的内容滚动体验(附源码)
前端·vue.js
编程修仙10 分钟前
第二篇 Vue指令
前端·javascript·vue.js·前端框架
frontend丶CV15 分钟前
useMemo
前端·react.js
明月_清风16 分钟前
基于 node-rtsp-stream 的 Web 直播方案详解
前端
DEMO派20 分钟前
前端处理用户离开当前页面的方案及对比解析
前端
LFly_ice20 分钟前
Next-4-路由导航
开发语言·前端·javascript
chilavert31821 分钟前
技术演进中的开发沉思-267 Ajax:拖放功能
前端·javascript·ajax
守护砂之国泰裤辣23 分钟前
el-select 选项偏移 到左边 左上角
前端·javascript·vue.js