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

相关推荐
坐吃山猪2 分钟前
Electron04-系统通知小闹钟
开发语言·javascript·ecmascript
小飞侠在吗9 分钟前
vue toRefs 与 toRef
前端·javascript·vue.js
csuzhucong12 分钟前
斜转魔方、斜转扭曲魔方
前端·c++·算法
燃烧的土豆27 分钟前
100¥ 实现的React项目 Keep-Alive 缓存控件
前端·react.js·ai编程
半生过往28 分钟前
前端运行PHP 快速上手 使用 PHPStudy Pro 详细搭建与使用指南
开发语言·前端·php
zlpzlpzyd30 分钟前
ecmascript中Promise和async/await的区别
开发语言·前端·ecmascript
streaker30330 分钟前
从零实现一个“类微信”表情输入组件
前端·vue.js·element
小明记账簿_微信小程序34 分钟前
js、node.js获取指定文件下的内容
前端
小明记账簿_微信小程序35 分钟前
h5中弹框出现后禁止页面滚动
前端
一个有故事的男同学42 分钟前
从零打造专业级前端 SDK (一):架构与工程化
前端·架构