adam84 Posted July 13, 2007 Share Posted July 13, 2007 I want to see how much time has elapse from the beginnning of the function to the end of the function. But I have having trouble. Every time I use the alert to display the sTime (start time) and eTime (end time) they are the same. I have even tried to put a for loop of a million to see if any time has elapsed but the two value still eqwual the same. Any ideas??? function timeElapsed(){ var tDate = new Date(); var sTime = tDate.getTime(); ~~~ Code var eTime = tDate.getTime(); alert(sTIme + ' ' + eTime); } Quote Link to comment https://forums.phpfreaks.com/topic/59799-javascript-time/ Share on other sites More sharing options...
RichardRotterdam Posted July 13, 2007 Share Posted July 13, 2007 this might help you http://www.javascript-page.com/timer.html Quote Link to comment https://forums.phpfreaks.com/topic/59799-javascript-time/#findComment-297332 Share on other sites More sharing options...
adam84 Posted July 13, 2007 Author Share Posted July 13, 2007 That was one of the articles which I read. The article used the settimeout function which will pause the sequence of the code for x amount of time. What I want to do is see how long it take to get from part A (beginning of the function) to part b (end of the function). I dont want to pause the code from running but time the function Quote Link to comment https://forums.phpfreaks.com/topic/59799-javascript-time/#findComment-297347 Share on other sites More sharing options...
RichardRotterdam Posted July 13, 2007 Share Posted July 13, 2007 You can use my class if you like but it does require mootoolshttp://mootools.net/download be sure to have <b>Native->functions</b> selected when you download it once you downloaded it include it into your HTML then paste my js into your head section <script language="JavaScript"> //Timer class function Timer(){ //object variables this.startTime; this.passedTime; this.currenTime; /*--Object functions--*/ //starts the timer this.startTimer=function(){ this.startTime=new Date(); this.passedTime=new Date(); //needs delegate thats why the this value is passed this function is called every second this.increaseTime.periodical(1000,this); }; //increases the time this.increaseTime=function(){ //each time the function is called the current time will be checked this.currenTime=new Date(); //calculates the time that has passed this.passedTime.setTime(this.currenTime.getTime()-this.startTime.getTime()); //$('time').value=this.passedTime.getMinutes()+":"+this.passedTime.getSeconds(); }; //returns the time in a string this.getPassedTime=function(){ //extra variables to show a neat time to return var seconds; var minutes; var hours; //nice display of seconds into a string if(this.passedTime.getSeconds()<10) { seconds="0"+String(this.passedTime.getSeconds()); }else{ seconds=String(this.passedTime.getSeconds()); } //nice display of minutes into a string if(this.passedTime.getMinutes()<10) { minutes="0"+String(this.passedTime.getMinutes()); }else{ minutes=String(this.passedTime.getMinutes()); } //returns the minutes:seconds in a string return minutes+":"+seconds; } } </script> then when you start your function do the following function yourFunction(){ //create a Timer Object var timer=new Timer(); timer.startTimer();//starts the timer } and when the function is done just call the function timer.getPassedTime() //alert the time that has passed when the function started alert(timer.getPassedTime()); Quote Link to comment https://forums.phpfreaks.com/topic/59799-javascript-time/#findComment-297426 Share on other sites More sharing options...
nogray Posted July 13, 2007 Share Posted July 13, 2007 You need to initiate two Date variables to get the different times, here is an example <script language="javascript"> function do_something(){ // start timer var st_time = new Date(); // do stuff var foo = 0; for (i=0; i<1000; i++){ foo += foo * i; } // end timer var en_time = new Date(); alert(st_time.getTime()+" "+en_time.getTime()); } do_something(); </script> Quote Link to comment https://forums.phpfreaks.com/topic/59799-javascript-time/#findComment-297472 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.