为啥我的是向前加一秒?
为啥我的是向前加一秒?
var RADIUS = 8; var MARGIN_LEFT=60; var MARGIN_TOP=30; const endTime = new Date(2017,8,19,13,07,52); var showtime=0; var ball = {x:512,y:100,r:20,g:2,vx:-4,vy:0,color:"#005588"}; var balls = []; const colors = ["#33B5E5","#0099CC","#AA66CC","#9933CC","#99CC00","#669900","#FFBB33","#FF8800","#FF4444","#CC0000"]; window.onload=function(){ var c= document.getElementById("canvas"); var ctx = c.getContext("2d"); showtime=getsecond(); setInterval( function(){ drawball(ctx); render(ctx); update(); },50) } function update(){ var nextsecond=getsecond(); var nextHours = parseInt( nextsecond / 3600); var nextMinutes = parseInt( (nextsecond - nextHours * 3600)/60 ) var nextSeconds = nextsecond % 60 var curHours = parseInt( showtime / 3600); var curMinutes = parseInt( (showtime - curHours * 3600)/60 ) var curSeconds = showtime % 60 if( nextSeconds != curSeconds ){ showtime = nextsecond; } } function getsecond(){ var curTime = new Date(); var ret = curTime.getTime()-endTime.getTime(); ret = Math.round(ret/1000); return ret>=0?ret:0; } function drawball(ctx){ ctx.clearRect(0,0,1024,768); ctx.beginPath(); ctx.fillStyle=ball.color; ctx.arc(ball.x,ball.y,ball.r,0,2*Math.PI); ctx.fill(); } function render(ctx){ var hours=parseInt(showtime/3600); var minutes=parseInt((showtime-hours*3600)/60); var seconds=parseInt(showtime%60); renderDigit(MARGIN_LEFT,MARGIN_TOP,parseInt(hours/10),ctx); renderDigit(MARGIN_LEFT+15*(RADIUS+1),MARGIN_TOP,parseInt(hours%10),ctx); renderDigit(MARGIN_LEFT+30*(RADIUS+1),MARGIN_TOP,10,ctx); renderDigit(MARGIN_LEFT+39*(RADIUS+1),MARGIN_TOP,parseInt(minutes/10),ctx); renderDigit(MARGIN_LEFT+54*(RADIUS+1),MARGIN_TOP,parseInt(minutes%10),ctx); renderDigit(MARGIN_LEFT+69*(RADIUS+1),MARGIN_TOP,10,ctx); renderDigit(MARGIN_LEFT+78*(RADIUS+1),MARGIN_TOP,parseInt(seconds/10),ctx); renderDigit(MARGIN_LEFT+93*(RADIUS+1),MARGIN_TOP,parseInt(seconds%10),ctx); } function renderDigit(x,y,num,ctx){ ctx.fillStyle="rgb(0,102,153)"; for(var i=0;i<digit[num].length;i++){ for(var j=0;j<digit[num][i].length;j++){ if (digit[num][i][j]==1){ ctx.beginPath(); ctx.arc(x+j*2*(RADIUS+1)+(RADIUS+1),y+i*2*(RADIUS+1)+(RADIUS+1),RADIUS,0,2*Math.PI); ctx.closePath(); ctx.fill(); } } } }