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

相关推荐
JIngJaneIL9 分钟前
基于springboot + vue房屋租赁管理系统(源码+数据库+文档)
java·开发语言·前端·数据库·vue.js·spring boot·后端
天天扭码11 分钟前
以浏览器多进程的角度解构页面渲染的整个流程
前端·面试·浏览器
你们瞎搞26 分钟前
Cesium加载20GB航测影像.tif
前端·cesium·gdal·地图切片
南山安1 小时前
Tailwind CSS:顺风CSS
javascript·css·react.js
milanleon1 小时前
使用Spring Security进行登录认证
java·前端·spring
excel1 小时前
🚀 从零开始:如何从 GPTsAPI.net 申请 API Key 并打造自己的 AI 服务
前端
期待のcode2 小时前
@RequestBody的伪表单提交场景
java·前端·vue.js·后端
栀秋6662 小时前
防抖 vs 节流:从百度搜索到京东电商,看前端性能优化的“节奏哲学”
前端·javascript
一颗烂土豆2 小时前
vfit.js v2.0.0 发布:精简、语义化与核心重构 🎉
前端·vue.js·响应式设计
有意义2 小时前
深入防抖与节流:从闭包原理到性能优化实战
前端·javascript·面试