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 分钟前
# 关于初学者对于JS异步编程十大误区
前端·javascript·面试
玖釉-8 分钟前
解决PowerShell执行策略导致的npm脚本无法运行问题
前端·npm·node.js
Larcher42 分钟前
新手也能学会,100行代码玩AI LOGO
前端·llm·html
徐子颐1 小时前
从 Vibe Coding 到 Agent Coding:Cursor 2.0 开启下一代 AI 开发范式
前端
小月鸭1 小时前
如何理解HTML语义化
前端·html
jump6801 小时前
url输入到网页展示会发生什么?
前端
诸葛韩信2 小时前
我们需要了解的Web Workers
前端
brzhang2 小时前
我觉得可以试试 TOON —— 一个为 LLM 而生的极致压缩数据格式
前端·后端·架构
yivifu2 小时前
JavaScript Selection API详解
java·前端·javascript
这儿有一堆花2 小时前
告别 Class 组件:拥抱 React Hooks 带来的函数式新范式
前端·javascript·react.js