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 小时前
🚀你以为你在写 React?其实你在“搭一套前端操作系统”
前端·react.js
爱上妖精的尾巴2 小时前
6-4 WPS JS宏 不重复随机取值应用
开发语言·前端·javascript
似水流年QC3 小时前
深入探索 WebHID:Web 标准下的硬件交互实现
前端·交互·webhid
陪我去看海3 小时前
测试 mcp
前端
speedoooo4 小时前
在现有App里嵌入一个AI协作者
前端·ui·小程序·前端框架·web app
全栈胖叔叔-瓜州4 小时前
关于llamasharp 大模型多轮对话,模型对话无法终止,或者输出角色标识User:,或者System等角色标识问题。
前端·人工智能
三七吃山漆4 小时前
攻防世界——wife_wife
前端·javascript·web安全·网络安全·ctf
用户47949283569154 小时前
面试官问"try-catch影响性能吗",我用数据打脸
前端·javascript·面试
GISer_Jing4 小时前
前端营销技术实战:数据+AI实战指南
前端·javascript·人工智能
GIS之路5 小时前
使用命令行工具 ogr2ogr 将 CSV 转换为 Shp 数据(二)
前端