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

相关推荐
吴文周13 分钟前
告别重复劳动:一套插件让 AI 替你写代码、修Bug、做测试、上生产
前端·后端·ai编程
Mh40 分钟前
我决定写一个 3D 地球仪来记录下我要去的地方
前端·javascript·动效
yaoxin5211231 小时前
390. Java IO API - WatchDir 示例
java·前端·python
懒狗小前端1 小时前
做了一个 codex 的中文文档网站,做的不好可以随便喷
前端·后端
. . . . .2 小时前
ref、useRef 和 forwardRef
前端·javascript·react.js
energy_DT2 小时前
2026年海上钻井平台数字孪生平台:引领海洋能源数字化转型
前端
Eric_见嘉3 小时前
在职前端 Agent 配置分享
前端·后端·agent
柚子8163 小时前
break跳出语句块的神奇技巧
前端·javascript
ejinxian4 小时前
Rust GUI框架Azul与Electron、WebView2
前端·javascript·electron
IT_陈寒5 小时前
Vue的v-for里用index当key,我被自己坑惨了
前端·人工智能·后端