473,288 Members | 1,726 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,288 software developers and data experts.

Calling function on function return

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
2 1384
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Ascaron | last post by:
Hi there! I’m having a strange problem with a c++ dll that is called from a c# program. The dll wraps a large piece of c++ software that uses exceptions for its error-signalling. To keep the...
12
by: Eric Lilja | last post by:
Hello, I'm working with a hash table that is encapsulated in a class. One of its member functions insert() throws an exception if the insertion fails (for example, if the value was already present...
5
by: Francesco Bochicchio | last post by:
Hi all, anybody knows if there is a (standard, portable) way to dinamically build a list of parameters to call a C function? Something like va_start & co, but to be used on the calling side? ...
14
by: ericellsworth | last post by:
Hi, I'm trying to use a class to pass variables back and forth from a form opened in dialog mode. I have created a class which invokes a form in its show method, like so: Public Sub Show() '...
16
by: aarklon | last post by:
Hi folks, recently i read the book named assembly language step by step by Jeff Duntemann. in the chapter coding for linux, he has got a paragraph named C calling conventions, which he...
9
by: Angel | last post by:
Hi again, I'm trying to call functions from a proprietary DLL but it's turned out to be more difficult than I thought. I have this W32.DLL which was written in C by USPS. They don't provide the...
2
by: Geler | last post by:
A theoretical question: Sorry if its a beginner question. Here is a quote from the MSDN explaning the C/C++ calling convention.. It demonstrates that the calling function is responsible to clean...
12
by: KIRAN | last post by:
hi, the grammer for any programming language says that when a function is called by another function,the callee after executing it's body should return to the point where it left in the caller.....
16
by: teju | last post by:
hi, i am trying 2 merge 2 projects into one project.One project is using c language and the other one is using c++ code. both are working very fine independently.But now i need to merge both...
13
by: Steve | last post by:
On page 392 of "Javascript the definitive guide" a function is called like this:- <form action="processform.cgi" onsubmit="return validateForm();"> Why, in this instance, is the return...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: marcoviolo | last post by:
Dear all, I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.