星空软件

 找回密码
 立即注册

[营销干货] js怎么实现倒计时,结合html,我给你演示一下

ibiraiyufa | 2023-11-6 19:37:15 | 显示全部楼层 |阅读模式
在JavaScript中,实现倒计时效果的一个常见方法是使用 setInterval 函数。这个函数可以让你设置一个时间间隔,然后在每个间隔结束时执行一个函数。在倒计时的情况下,你可以在每个时间间隔减少计时器的时间,并更新显示的时间。
下面是一个简单的HTML和JavaScript代码示例,展示了如何实现一个简单的倒计时效果:


<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Simple Countdown Timer</title>
<script>
function startCountdown(duration, display) {
    var timer = duration, minutes, seconds;
    var countdownInterval = setInterval(function () {
        minutes = parseInt(timer / 60, 10);
        seconds = parseInt(timer % 60, 10);

        minutes = minutes < 10 ? "0" + minutes : minutes;
        seconds = seconds < 10 ? "0" + seconds : seconds;

        display.textContent = minutes + ":" + seconds;

        if (--timer < 0) {
            clearInterval(countdownInterval);
            display.textContent = "Time's up!";
        }
    }, 1000);
}

window.onload = function () {
    var countdownButton = document.getElementById('start-countdown');
    var timeDisplay = document.getElementById('time');
    var duration = 60; // Duration in seconds

    countdownButton.onclick = function () {
        startCountdown(duration, timeDisplay);
    };
};
</script>
</head>
<body>
<div>
    <span id="time">01:00</span>
    <button id="start-countdown">Start Countdown</button>
</div>
</body>
</html>

然后我们把这个代码在本地测试演示一下给大家
1、先创建一个1.html的网页文档
截图202311061938089710.png
2、然后把上面写好的代码粘贴进去
截图202311061938488738.png
3、然后我们打开1.html网页,点击那个按钮后发现时间就在倒计时
截图202311061939387503.png




上一篇:php实现读取根目录下所有文件名,包括目录,完整代码
下一篇:js多线程实现get请求,对一个网址
回复

使用道具 举报

ixugidade | 2023-11-6 19:39:50 | 显示全部楼层
学习了
回复

使用道具 举报

ycipujyzy | 2023-11-6 19:39:55 | 显示全部楼层
原来这么简单啊
回复

使用道具 举报

odisedox | 2023-11-6 19:40:25 | 显示全部楼层
确实挺简单的
回复

使用道具 举报

Shen | 2023-11-6 19:40:41 | 显示全部楼层
666
回复

使用道具 举报

yuxin_3 | 2023-11-6 19:40:45 | 显示全部楼层
支持支持
回复

使用道具 举报

dahamaa | 2023-11-6 19:40:50 | 显示全部楼层
我喜欢
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

全站声明【必看】|小黑屋|新库软件 |网站地图

GMT+8, 2024-4-29 10:02 , Processed in 0.069655 second(s), 25 queries .

Powered by Discuz! X3.4

© 2001-2017 Comsenz Inc.