By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,374 Members | 1,789 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,374 IT Pros & Developers. It's quick & easy.

Calling function on function return

P: n/a
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
Jun 26 '07 #1
Share this Question
Share on Google+
2 Replies


P: n/a
On Jun 26, 9:01 pm, Emil <e...@poczta.onet.plwrote:
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/ >
--
Rob

Jun 27 '07 #2

P: n/a
RobG pisze:
On Jun 26, 9:01 pm, Emil <e...@poczta.onet.plwrote:
>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/ >
--
Rob
That's the idea I was searching for. Thanks.
Emil
Jun 27 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.