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()的参数

相关推荐
司宸3 分钟前
学习笔记八 —— 虚拟DOM diff算法 fiber原理
前端
阳树阳树3 分钟前
JSON.parse 与 JSON.stringify 可能引发的问题
前端
让辣条自由翱翔8 分钟前
总结一下Vue的组件通信
前端
dyb9 分钟前
开箱即用的Next.js SSR企业级开发模板
前端·react.js·next.js
前端的日常10 分钟前
Vite 如何处理静态资源?
前端
前端的日常11 分钟前
如何在 Vite 中配置路由?
前端
兮漫天11 分钟前
bun + vite7 的结合,孕育的 Robot Admin 靓仔出道(一)
前端
PineappleCoder12 分钟前
JS 作用域链拆解:变量查找的 “俄罗斯套娃” 规则
前端·javascript·面试
兮漫天12 分钟前
bun + vite7 的结合,孕育的 Robot Admin 靓仔出道(二)
前端
用户479492835691517 分钟前
面试官:为什么很多格式化工具都会在行尾额外空出一行
前端