十秒倒计时开始停止按钮

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>倒计时10秒按钮</title>
</head>
<body>
	<button "start()">开始</button>
	<button "stop()">停止</button>
	<strong id="time">10</strong>

</body>
<script type="text/javascript">
	
	var times = null;
	function start(){
		time.innerHTML=10;
		if (times!= null) {
			return false
		}
		var num = 10;
		times =setInterval(function(){
			num--;
			time.innerHTML=num;
			if (num==0) {
				stop();
			}			
		},1000)
	}
	
	function stop(){
		alert("倒计时停止");
		clearInterval(times);
		times = null;

	}
</script>
</html>
  • 3
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
这是一个基于Arduino的示例代码,使用了一个4位数码管、一个按钮一个蜂鸣器。请确保你已经连接好了这些硬件设备。 ```c++ #include <SevSeg.h> SevSeg sevseg; // 数码管对象 const int buttonPin = 2; // 按钮引脚 const int buzzerPin = 3; // 蜂鸣器引脚 int countdown = 0; // 倒计时间() bool countdownStarted = false; // 是否已经开始倒计时 bool countdownStopped = false; // 是否已经停止倒计时 bool countdownFinished = false; // 是否已经完成倒计时 void setup() { byte numDigits = 4; // 数码管显示4位数字 byte digitPins[] = {4, 5, 6, 7}; // 数码管的4个引脚 byte segmentPins[] = {8, 9, 10, 11, 12, 13, A0, A1}; // 数码管的7个段的引脚 bool resistorsOnSegments = false; // 数码管的段是否接上了电阻 bool updateWithDelays = false; // 是否使用延更新数码管的显示 bool leadingZeros = false; // 是否在前面补0 bool disableDecPoint = true; // 是否禁用小数点 sevseg.begin(COMMON_ANODE, numDigits, digitPins, segmentPins, resistorsOnSegments, updateWithDelays, leadingZeros, disableDecPoint); pinMode(buttonPin, INPUT_PULLUP); // 按钮使用内置上拉电阻 pinMode(buzzerPin, OUTPUT); // 蜂鸣器输出 Serial.begin(9600); // 打开串口以便调试 } void loop() { // 读取按钮状态 bool buttonState = digitalRead(buttonPin) == LOW; // 如果按钮被按下并且倒计时没有开始,则设置倒计时间 if (buttonState && !countdownStarted) { countdown = 10; // 设置倒计时间为10 countdownStarted = true; // 标记已经开始倒计时 countdownStopped = false; // 标记没有停止倒计时 countdownFinished = false; // 标记没有完成倒计时 } // 如果按钮被按下并且倒计时已经开始,则停止倒计时并重新设置倒计时间 if (buttonState && countdownStarted && !countdownStopped) { countdownStopped = true; // 标记已经停止倒计时 countdown = 5; // 重新设置倒计时间为5 } // 更新数码管的显示 if (!countdownFinished) { sevseg.setNumber(countdown); // 显示倒计时间 sevseg.refreshDisplay(); // 刷新数码管的显示 } // 如果倒计时已经开始并且没有被停止,则倒计时减1 if (countdownStarted && !countdownStopped && !countdownFinished) { countdown--; // 倒计时减1 if (countdown == 0) { countdownFinished = true; // 标记已经完成倒计时 digitalWrite(buzzerPin, HIGH); // 开启蜂鸣器 delay(3000); // 等待3 digitalWrite(buzzerPin, LOW); // 关闭蜂鸣器 } } // 调试信息 Serial.print("Countdown: "); Serial.println(countdown); Serial.print("Countdown started: "); Serial.println(countdownStarted); Serial.print("Countdown stopped: "); Serial.println(countdownStopped); Serial.print("Countdown finished: "); Serial.println(countdownFinished); Serial.println(); } ``` 这个代码主要是通过读取按钮的状态来设置倒计时间,并且可以在倒计时过程按下按钮停止倒计时并重新设置倒计时间。同,它还会在倒计时结束开启蜂鸣器响3。你可以根据需要修改代码来适应自己的硬件环境。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值