首页 > 编程知识 正文

js如何制作网页时钟(js怎么做动态时钟)

时间:2023-11-29 19:12:35 阅读:310287 作者:BJFF

本文目录一览:

  • 1、制作页面时钟,在页面内显示当前时间,并使用计时器控制时间的变化用JAVASCRIPT
  • 2、如何用javascript实现一个时钟?
  • 3、怎么用js让网页显示时间
  • 4、怎么把js时钟放在网页侧边
  • 5、javascript,实现一个时钟,页面显示当前时间包括年月日时 分 秒 并设定一个时间点,当该
  • 6、网页制作中时间和日期怎么加。

制作页面时钟,在页面内显示当前时间,并使用计时器控制时间的变化用JAVASCRIPT

// JavaScript Document

function disptime(){

var today=new Date();

var hh=today.getHours();

var mm=today.getMinutes();

var ss=today.getSeconds();

document.getElementById("myClock").innerHTML="h1现在的时间是:"+hh+":"+mm+":"+ss+"h1";

}

var timer;

function interval(){

timer=setInterval("disptime()",1000);

}

在html文件中引入和加载就好了,引入总会的吧。

如何用javascript实现一个时钟?

function init(){

  clock();

  setInterval(clock,1000);

}

function clock(){

  var now = new Date();

  var ctx = document.getElementById('canvas').getContext('2d');

  ctx.save();

  ctx.clearRect(0,0,150,150);

  ctx.translate(75,75);

  ctx.scale(0.4,0.4);

  ctx.rotate(-Math.PI/2);

  ctx.strokeStyle = "black";

  ctx.fillStyle = "white";

  ctx.lineWidth = 8;

  ctx.lineCap = "round";

  // Hour marks

  ctx.save();

  for (var i=0;i12;i++){

    ctx.beginPath();

    ctx.rotate(Math.PI/6);

    ctx.moveTo(100,0);

    ctx.lineTo(120,0);

    ctx.stroke();

  }

  ctx.restore();

  // Minute marks

  ctx.save();

  ctx.lineWidth = 5;

  for (i=0;i60;i++){

    if (i%5!=0) {

      ctx.beginPath();

      ctx.moveTo(117,0);

      ctx.lineTo(120,0);

      ctx.stroke();

    }

    ctx.rotate(Math.PI/30);

  }

  ctx.restore();

  

  var sec = now.getSeconds();

  var min = now.getMinutes();

  var hr  = now.getHours();

  hr = hr=12 ? hr-12 : hr;

  ctx.fillStyle = "black";

  // write Hours

  ctx.save();

  ctx.rotate( hr*(Math.PI/6) + (Math.PI/360)*min + (Math.PI/21600)*sec )

  ctx.lineWidth = 14;

  ctx.beginPath();

  ctx.moveTo(-20,0);

  ctx.lineTo(80,0);

  ctx.stroke();

  ctx.restore();

  // write Minutes

  ctx.save();

  ctx.rotate( (Math.PI/30)*min + (Math.PI/1800)*sec )

  ctx.lineWidth = 10;

  ctx.beginPath();

  ctx.moveTo(-28,0);

  ctx.lineTo(112,0);

  ctx.stroke();

  ctx.restore();

  

  // Write seconds

  ctx.save();

  ctx.rotate(sec * Math.PI/30);

  ctx.strokeStyle = "#D40000";

  ctx.fillStyle = "#D40000";

  ctx.lineWidth = 6;

  ctx.beginPath();

  ctx.moveTo(-30,0);

  ctx.lineTo(83,0);

  ctx.stroke();

  ctx.beginPath();

  ctx.arc(0,0,10,0,Math.PI*2,true);

  ctx.fill();

  ctx.beginPath();

  ctx.arc(95,0,10,0,Math.PI*2,true);

  ctx.stroke();

  ctx.fillStyle = "#555";

  ctx.arc(0,0,3,0,Math.PI*2,true);

  ctx.fill();

  ctx.restore();

  ctx.beginPath();

  ctx.lineWidth = 14;

  ctx.strokeStyle = '#325FA2';

  ctx.arc(0,0,142,0,Math.PI*2,true);

  ctx.stroke();

  ctx.restore();

}

怎么用js让网页显示时间

$(function(){

function getTime(){

var date=new Date();

var time=date.getHours()+":"+date.getMinutes()+":"+date.getSeconds();

$('div').html(time);

}

setInterval(getTime, 1000)

});

页面中放一个div显示时间

怎么把js时钟放在网页侧边

1、首先打开电脑。

2、其次在电脑主页找到并点击设置。

3、最后在设置中点击时钟设置,点击侧面即可。

javascript,实现一个时钟,页面显示当前时间包括年月日时 分 秒 并设定一个时间点,当该

html

head

titleTime/title

/head

body

div id="time"/div

div id="alert"/div

/body

script type="text/javascript" charset="utf-8" async defer

var time = document.getElementById("time");

showTime();

function showTime() {

// To get the date time

var date = new Date();

var year = date.getYear() + 1900;

var month = date.getMonth() + 1;

var day = date.getDate();

var hour = date.getHours();

var min = date.getMinutes();

var sec = date.getSeconds();

var date_time = year + "-" + month + "-" + day + " " + hour + ":" + min + ":" + sec;

time.innerHTML = date_time;

// when the time is equal your condition, show the special words.

if(date_time == "2014-6-25 11:45:20") {

document.getElementById("alert").innerHTML = "It's time to display your words here.";

}

}

// set the interval time

setInterval(showTime, 1000);

/script

/html

网页制作中时间和日期怎么加。

添加的具体方法如下:

1、打开Dreamweaver新建一个html网页。

2、切换到代码视图,在head/head标签里,title/title标签后面输入JavaScript标签。  script type="text/javascript"/script

3、在script标签里定义一个time()函数

function time()

{

thistime=new Date()                                   //创建一个对象

var hours=thistime.getHours()                    //获取时钟

var minutes=thistime.getMinutes()             //获取分钟

var seconds=thistime.getSeconds()              //获取秒钟

var years=thistime.getYear()                         //获取年

var months=thistime.getMonth()                //获取月

var days=thistime.getDay()                          获取日

}

4、判断当时钟小于10的时候在前面加个0,例如8点零8分会显示08:08

if(eval(hours)10)

{

hours="0"+hours

}

if(eval(minutes10))

{

minutes="0"+minutes

}

if(seconds10)

{

seconds="0"+seconds

5、thistime=hours+":"+minutes+":"+seconds     //把时间显示顺序调整好

document.title=thistime                                //在标题显示

var timer=setTimeout("time()",1000)              //设置计时器,让时间每分每秒更新

6、在body/body标签加上加载触发事件

body onload="time()"

7、按Ctrl+s保存,按F12在浏览器中浏览。

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。