在不同的单元格中使用相同的Javascript来计算过去的天数

时间:2014-02-18 14:12:15

标签: javascript html counter

我目前正在尝试填充一个带有几个计数器的html表,一个在另一个下面,以显示自事件发生后的过去几天。 感谢互联网,我最终得到了这个脚本:

    <script language="JavaScript1.2" type="text/javascript">function
     setcountup(theyear,themonth,theday){
     yr=theyear;mo=themonth;da=theday
     }

   //////////CONFIGURE THE countup SCRIPT HERE//////////////////

   //STEP 1: Configure the date to count up from, in the format year, month, day:
   //This date should be less than today
   setcountup(2012,9,19)

   //STEP 2: Configure text to be attached to count up
   var displaymessage=""


   //STEP 3: Configure the below 5 variables to set the width, height, background color,       
   and text style of the countup area
   var countupwidth='90%'
   var countupheight='40px' //applicable only in NS4
   var countupbgcolor=''
   var opentags='<font face="Verdana"><large>'
   var closetags='</large></font>'

   //////////DO NOT EDIT PASS THIS LINE//////////////////

   var montharray=new    
   Array("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec")
   var crosscount=''

   function start_countup(){
   if (document.layers)
   document.countupnsmain.visibility="show"
   else if (document.all||document.getElementById)
   crosscount=document.getElementById&&!document.all?   
   document.getElementById("countupie") : countupie
   countup()
   }

   if (document.all||document.getElementById)
   document.write('<span id="countupie" style="width:'+countupwidth+'; background-    
   color:'+countupbgcolor+'"></span>')

   window.onload=start_countup


   function countup(){
   var today=new Date()
   var todayy=today.getYear()
   if (todayy < 1000)
   todayy+=1900
   var todaym=today.getMonth()
   var todayd=today.getDate()
   var todayh=today.getHours()
   var todaymin=today.getMinutes()
   var todaysec=today.getSeconds()
   var todaystring=montharray[todaym]+" "+todayd+", "+todayy+"     
   "+todayh+":"+todaymin+":"+todaysec
   paststring=montharray[mo-1]+" "+da+", "+yr
   paststring="10:00"+montharray[mo-1]+" "+da+", "+yr
   dd=Date.parse(todaystring)-Date.parse(paststring)
   dday=Math.floor(dd/(60*60*1000*24)*1)
   dhour=Math.floor((dd%(60*60*1000*24))/(60*60*1000)*1)
   dmin=Math.floor(((dd%(60*60*1000*24))%(60*60*1000))/(60*1000)*1)
   dsec=Math.floor((((dd%(60*60*1000*24))%(60*60*1000))%(60*1000))/1000*1)

   if (document.layers){
   document.countupnsmain.document.countupnssub.document.write(opentags+dday+ " days    
   "+displaymessage+closetags)//to get more detail, enter one of the following in  the   
   write line(also in the else): +dhour+" hours, "+dmin+" minutes, and "+dsec+" seconds    
   "
   document.countupnsmain.document.countupnssub.document.close()
   }
   else if (document.all||document.getElementById)
   crosscount.innerHTML=opentags+dday+ " days "+displaymessage+closetags//+dhour+"  
   hours, "+dmin+" minutes, and "+dsec+" seconds "

   setTimeout("countup()",1000)
   }
   </script>

现在,每个单元格行都有一个不同事件的计数器。 我似乎无法将这些代码放在每个代码中,因为它会产生冲突(我认为)

我是全新的,我必须解决这个问题。 有人可以帮助我,或者指出我正确的方向吗?

提前谢谢

1 个答案:

答案 0 :(得分:1)

我花了很多时间试图让你发布的代码工作,我不得不说,这是一些非常糟糕的代码。有很多不需要的代码,如:

paststring=montharray[mo-1]+" "+da+", "+yr
paststring="10:00"+montharray[mo-1]+" "+da+", "+yr

和许多只是一般的格式化疯狂。我相信我已经使它工作了,它比阅读起来容易得多。这个小提琴可以帮助你让你的工作。

http://jsfiddle.net/9DNaD/