Time taken for completing a process by using getTime()

We can calculate total time taken by a process by using getTime() method. This is a three step program.

First we will create the current date object and store the getTime() value in milliseconds in a variable.
var my_time1 = new Date(); // date object 
my_time1=my_time1.getTime(); // first time variable
Then we will keep the system busy by creating a loop and create some time delay.
for(i=1; i<=100000000; i++){
if((i % 10000) == 0){
document.getElementById('display1').innerHTML=i;
}
}
Third step is to create date object and read the getTime() method again.
var my_time2 = new Date(); // date object 
my_time2=my_time2.getTime(); // second time variable
The difference in first getTime value and present getTime value will give us the duration the system has taken to complete the looping process.
var diff = ( my_time2-my_time1); // difference in time 
document.getElementById('display2').innerHTML = 'The process took ' + diff + ' Milliseconds';

Process time by using getTime()

Here is the complete code.
<html>
<head>
<title>Demo of getTime function</title>
	</head>
<body>
<script type="text/javascript"> 
function show_gettime(){
var my_time1 = new Date(); // date object 
my_time1=my_time1.getTime(); // first time variable

for(i=1; i<=100000000; i++){
if((i % 10000) == 0){
document.getElementById('display1').innerHTML=i;
}
}

var my_time2 = new Date(); // date object 
my_time2=my_time2.getTime(); // second time variable

var diff = ( my_time2-my_time1); // difference in time 
document.getElementById('display2').innerHTML = 'The process took ' + diff + ' Milliseconds';
}
</script>
<input type=button onclick='show_gettime()' value='Start';>
<div id='display1'></div>
<br><br>
<div id='display2'></div>

</body>
</html>


plus2net.com




Post your comments , suggestion , error , requirements etc here .




We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
©2000-2020 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer