Jump to content

Javascript Time


adam84

Recommended Posts

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);
}

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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());

Link to comment
Share on other sites

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>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.