1. Code that is not encapsulated is very confusing.
<style>input{width:300px;height:20px;margin:10px} #btn {Width:150px;height:30px;margin-left:65px}</style> <script>window.onload=function(){ varInputs = document.getElementsByTagName (' input '); varTimer=NULL; varseconds=NULL; varCurrentdate=NULL; varStr= "; varTargetDate =NewDate (Inputs[0].value);//the target time is fixed.inputs[2].onclick=function() {clearinterval (timer); Timer=setinterval (function(){ if(seconds>=0) {currentdate=NewDate ();//Current Timeseconds = Math.floor ((targetdate-currentdate)/1000);//milliseconds converted to S Str=math.floor (seconds/86400) + ' Day ' +math.f Loor (seconds%86400/3600) + ' +math.floor (seconds%86400%3600/60) + ' +seconds%60+ ' seconds '; Inputs[1].value=str; }Else{clearinterval (timer); } },1000); } } </script>Distance from: <input type= "text" value= "June 7,2016 00:00:00" ><br>still left:<input type= "text" ><br><input id= "btn" type= "button" value= "Start rewinding" ></body>
2. Post-packaged code, faster and better
<script>window.onload=function(){ varInputs = document.getElementsByTagName (' input '); varTimer=NULL; varDelta=NULL; varCurrentdate=NULL; varStr= "; varTargetDate =NewDate (Inputs[0].value);//the target time is fixed.inputs[2].onclick=function() {firstget ();//otherwise the page appears to stutter, this is up to display the time information into the input boxclearinterval (timer); Timer=setinterval (firstget,1000); functionFirstget () {if(delta>=0) {currentdate=NewDate ();//Current TimeDelta =targetdate-currentdate; STR=getdayhourminsecondbyms (delta); inputs[1].value=str; }Else{clearinterval (timer); } } } } functiongetdayhourminsecondbyms (ms) {varseconds = Math.floor (ms/1000);//milliseconds converted to SreturnMath.floor (seconds/86400) + ' Day ' +math.floor (seconds%86400/3600) + ' time ' +math.floor (seconds%86400%3600/60) + ' min ' +seconds %60+ ' seconds '; } </script>
2016-06-02 Timer Countdown