Archive for July, 2008

Javascript script timer (see what the execution time is)

The title of this post says it all.
I made a MINI script timer object that counts how long a certain piece of code runs (executes).
It’s very handy for optimizing youre code!

The output is displayed in seconds with 3 digits behind the comma.
if you just want milliseconds remove the “/1000″ from:

this.scriptRunTime = (this.stopTime - this.startTime)/1000;

The code:

/**
 * javascript script timer
 * example:
 * scriptTimer.startTimer();
 * alert('hello');
 * scriptTimer.stopTimer();
 * alert(scriptTimer.scriptRunTime);
 *
 */
var scriptTimer = {
	scriptRunTime : 0,
	startTime : 0,
	stopTime : 0,
	startTimer : function(){
		time = new Date();
		this.startTime = time.getTime();
	},
	stopTimer : function(){
		time = new Date();
		this.stopTime = time.getTime();
		this.scriptRunTime = (this.stopTime - this.startTime)/1000;
	}
}

(continue reading…)


Javascript debugging (print_r/var_dump in javascript like PHP)

In javascript there is no function for printing variables like you have PHP, I use print_r and var_dump alot in PHP for dumping variables
so I wanted to create a simular function in javascript.

If you want to walk through unknown child objects in javascript you can use a operator called ‘in’ like this:

//Create a object
element = {child1: 'First child', child2: 'Second child'};

string = '';

//Loop through all the child objects in element
for(property in element)
{
	//Add the name and value of the child object
	string += property + ': '+ element[property] + ''+ '\n';
}
//Ouput the result
alert(string);

(continue reading…)


Copyright © 1996-2010 Re:morse.nl. All rights reserved.
iDream theme by Templates Next | Powered by WordPress