473,569 Members | 2,790 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1397
On Jun 26, 9:01 pm, Emil <e...@poczta.on et.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(f unc0, 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.on et.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(f unc0, 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
352
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 exception mechanism away from the c# end, all functions in the dll are embedded in catch-all blocks that return ‘false’ if anything throws an...
12
1861
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 in the hash table). Now I have client code that looks like this: bool exception_thrown = false; try { hash_table.insert(some_value);
5
3022
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? In other words, suppose that I have a generic pointer : void * f_ptr; I know that this pointer points to a fuction. I also know the function
14
2982
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() ' This method shows the form used to get the info If sWhereInt = "" Then DoCmd.OpenForm sFormNameInt, acNormal, , , acFormAdd, _
16
2285
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 describes as follows 1)
9
2808
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 code so I only have the documentation. I'm trying to call a function called z4date that, according to the docs, returns the date as "an 8-byte...
2
3139
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 the stack pointer and it does it by the command "add esp,8" after returning from the called function. My questions: 1. Is the stack pointer...
12
1285
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.. Is there any technique to make the callee to return to some other point(within the current process) other than the callee by changing the call...
16
517
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 and my c++ code should call c code.but when i tried to call a function in c code externing that function in my c++ code, i am getting unresolved...
13
7776
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 statement used in calling the function validateForm rather than being included inside the function? Thanks in advance,
0
7694
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7609
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7921
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8118
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
7964
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6278
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
3651
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
2107
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
936
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.