Calling function on function return

E

Emil

Is it posiible to create callback for a function, that is automatically
called before that function returns? I need it for tracing/profiling
purposes.

eg.:

var execTime = null;
var startTime = null;

function callback() {
execTime = (new Date).getTime() - startTime;
}

function fun() {
startTime = (new Date).getTime();

....
return; //callback should be automatically called before function returns

....
return;


....
return;

}

Of course it would be nice if another callback could be called when
function enters.

emil
 
R

RobG

Is it posiible to create callback for a function, that is automatically
called before that function returns? I need it for tracing/profiling
purposes.

Not in the way you want - javascript is single threaded. You can use
setTimeout to run a function at some later time, but:

1. It will not start until the current function is finished

2. If some other javscript function is running when the time
is up, it will wait at least until that function has finished

You may be able to do what you want using a wrapper that calls other
functions and times how long they take, e.g.

function timingWrapper(func0, func1, ...){

var s0, s1, s2, ...
var f0, f1, f2, ...

// Time first function
s0 = new Date();
func0();
f0 = new Date();

// Time second function
s1 = new Date();
func1();
f1 = new Date();

...

alert('1st took ' + (f0-s0) + ' milliseconds'
+ '\n2nd took ' + (f1-s1) + ' milliseconds'
+ ...
);
}

You can extend that approach using an object that stores required
values and calls itself (maybe using setTimeout) when functions are
finished. You can also run stuff in an iframe to get a kind of
isolated environment.

Have a look at the techniques applied here:

<URL: http://mootools.net/slickspeed/ >
 
E

Emil

RobG pisze:
Not in the way you want - javascript is single threaded. You can use
setTimeout to run a function at some later time, but:

1. It will not start until the current function is finished

2. If some other javscript function is running when the time
is up, it will wait at least until that function has finished

You may be able to do what you want using a wrapper that calls other
functions and times how long they take, e.g.

function timingWrapper(func0, func1, ...){

var s0, s1, s2, ...
var f0, f1, f2, ...

// Time first function
s0 = new Date();
func0();
f0 = new Date();

// Time second function
s1 = new Date();
func1();
f1 = new Date();

...

alert('1st took ' + (f0-s0) + ' milliseconds'
+ '\n2nd took ' + (f1-s1) + ' milliseconds'
+ ...
);
}

You can extend that approach using an object that stores required
values and calls itself (maybe using setTimeout) when functions are
finished. You can also run stuff in an iframe to get a kind of
isolated environment.

Have a look at the techniques applied here:

<URL: http://mootools.net/slickspeed/ >
That's the idea I was searching for. Thanks.
Emil
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top