jquery 使用计时器显示当前时间和停止当前时间
2021/4/17 18:28:50
本文主要是介绍jquery 使用计时器显示当前时间和停止当前时间,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> <script src="./js/jquery-3.6.0.js"></script> <script> $(function(){ var timer; // 为btnStart按钮绑定事件 开始显示 $("#btnStart").click(function(){ //启动计时器 timer=setInterval(()=>{ // 获取当前时间 var now=new Date(); var year=now.getFullYear(); var month=now.getMonth()+1; var day=now.getDate(); var hour=now.getHours(); var minute=now.getMinutes(); var second=now.getSeconds(); var currentTime=year+"年"+month+"月"+day+"日"+hour+"时"+minute+"分"+second+"秒"; $("#currentTime").text(currentTime); },1000) }) // 为btnStop按钮绑定事件 停止显示 $("#btnStop").click(function(){ // 停止计时器 clearTimeout(timer) }) }) </script> </head> <body> <button id="btnStart">开始显示当前时间</button> <button id="btnStop">停止显示当前时间</button> <h2 id="currentTime"></h2> </body> </html>
这篇关于jquery 使用计时器显示当前时间和停止当前时间的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-03-06jquery对css样式(jquery中的css方法)-icode9专业技术文章分享
- 2023-05-27JQuery的认识和安装
- 2023-01-06JQuery应用技巧:如何定义 HTML 模板并使用 JQuery 进行加载-icode9专业技术文章分享
- 2022-09-29复习-jQuery
- 2022-09-04Python3项目初始化10-->前端基础jquery、ajax,sweetalert--更新用户改造
- 2022-08-30day 27 jquery
- 2022-08-29jQuery筛选器,bootstrap
- 2022-08-20JQuery事件绑定
- 2022-08-20JQuery案例
- 2022-08-07关于jQuery的学习