Home > Web Front-end > JS Tutorial > body text

Simple example (JS implements web clock)

亚连
Release: 2018-05-18 15:08:21
Original
1935 people have browsed it

The following is an example of using JS to implement a web page clock that I compiled for you. Interested students can take a look.

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>网页时钟</title>
<script type="text/javascript">
function displayTime(){
//1.获取div元素
var timeDiv=document.getElementById("timeDiv");
//2.获取系统当前时间
var nowTime=new Date();
var strNowTime=nowTime.toLocaleString();
//3.将系统时间设置到div元素中
timeDiv.innerHTML=strNowTime;
}
//每隔1秒调用一次displayTime函数
function start(){
window.setInterval("displayTime()",1000)//单位是毫秒
}
</script>
</head>
<!--  body onload:当整个html页面加载完成后执行此函数  -->
<body onload="start();">
<div id="timeDiv"></div>
</body>
</html>
Copy after login

The above is what I compiled for everyone. I hope it will be helpful to everyone in the future.

Related articles:

Detailed explanation of Javascript’s closure working principle

Detailed explanation of webpack-dev -Simple use of server_javascript skills

Detailed explanation of this attribute in javascript

The above is the detailed content of Simple example (JS implements web clock). For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template