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

how to write userdefined callback function

P: 67
Hi,
i know we can wirte a userdefined callback function..

I had a try for a long time though i feel im near but not able to coin out the exact thing. can any body tell me about a user defined callback function... and a sample on how to write it...


looking forward for a reply..


Thanks in advance for your reply...
Sep 3 '07 #1
Share this Question
Share on Google+
6 Replies


pbmods
Expert 5K+
P: 5,821
Heya, New Learner.

You may find this article useful.
Sep 3 '07 #2

P: 67
Heya, New Learner.

You may find this article useful.

thx pbmods... The article was useful...

but my doubt is..

whenever http.onreadystatechange value is changed the assigned function is called...

similarly.. i need to create a userdefined handler for ex:
x = somefunction
like onreadystatechange(which is a browser object) whenever the value of x is changed thn somefunction should be initiated.

Is this Possible..


looking forward for a reply..

Thanks in advance
Sep 4 '07 #3

gits
Expert Mod 5K+
P: 5,384
hi ...

so x is a simple js-variable and you want to react with 'somefunction' when the value of x changes?

but i assume you already know WHERE x changes (in case its your own code)? ... everytime a value is assigned to x you may call 'somefunction' or use a set-function for x instead of a simple assignment ... and in that so called 'setter' you may call whatever you want ...

hope i didn't misunderstood your problem?

another method would be to define a watcher:

Expand|Select|Wrap|Line Numbers
  1. obj.watch(property, callback);
  2.  
and to detach the watcher:

Expand|Select|Wrap|Line Numbers
  1. obj.unwatch(property);
  2.  
kind regards
Sep 4 '07 #4

P: 67
hi ...

so x is a simple js-variable and you want to react with 'somefunction' when the value of x changes?

but i assume you already know WHERE x changes (in case its your own code)? ... everytime a value is assigned to x you may call 'somefunction' or use a set-function for x instead of a simple assignment ... and in that so called 'setter' you may call whatever you want ...

hope i didn't misunderstood your problem?

another method would be to define a watcher:

Expand|Select|Wrap|Line Numbers
  1. obj.watch(property, callback);
  2.  
and to detach the watcher:

Expand|Select|Wrap|Line Numbers
  1. obj.unwatch(property);
  2.  
kind regards
tnx for the idea...
I have tried out the way u suggested ... it works fine...

But my doubt is...
we write:
Expand|Select|Wrap|Line Numbers
  1. http.onreadystatechange=response;
response is triggered whenever onreadystatechange is chnged from 0 .. 4.

similarly... i write
Expand|Select|Wrap|Line Numbers
  1. function base() {
  2.   this.x=0;
  3. }
  4.  
  5. base = new base()
  6.  
  7. function tst(){
  8.   base.x = callfun;
  9. }
in a html page i have 2 buttons

on a 1st onclick event i say base.x=1// I need callfun to be triggered (coz x has changed)
on a 2nd onclick event i say base.x=2// I need callfun to be triggered (coz x has changed)

Is This possible...?


Looking for ward fir a reply..

Thanks in advance
Sep 4 '07 #5

acoder
Expert Mod 15k+
P: 16,027
Please use CODE tags when posting code, e.g.
[code=javascript]Javascript code goes here[/code.] (remove the dot).
Sep 4 '07 #6

gits
Expert Mod 5K+
P: 5,384
hi ...

have a look at the following example:

Expand|Select|Wrap|Line Numbers
  1. <script>
  2. function OBJ() {
  3.     this.x = 0;
  4. }
  5.  
  6. OBJ.prototype.increment_val = function() {
  7.     this.x++;
  8. }
  9.  
  10. var obj = new OBJ;
  11.  
  12. obj.watch('x', function(val, oldval, newval) {alert(newval); return newval;});
  13. </script>
  14.  
  15. <a href="#" onclick="obj.increment_val();">increase obj counter</a>
  16.  
kind regards

ps: note watch will not work with IE ... so the 'setter'-method is probably to be preferred :)
Sep 4 '07 #7

Post your reply

Sign in to post your reply or Sign up for a free account.