how to pass a parameter to some bound function?

M

Max

i have a event bind function like this(though it is not so robust):

bind$=function(o,evt,fn,cb){
var aE='attachEvent';
var aEL='addEventListener';
if(!o[aE]&&o[aEL]){
return o[aEL](evt,fn,!!cb);
}
return o[aE]('on'+evt,fn);
};

some usage like this:

bind$( /* o */ document, /* evt */ 'click', /* fn */ function(txt)
{alert(txt);} );

but how can I pass a parameter to the bound function "fn" ?
 
D

david.karr

i have a event bind function like this(though it is not so robust):

bind$=function(o,evt,fn,cb){
var aE='attachEvent';
var aEL='addEventListener';
if(!o[aE]&&o[aEL]){
return o[aEL](evt,fn,!!cb);
}
return o[aE]('on'+evt,fn);

};

some usage like this:

bind$( /* o */ document, /* evt */ 'click', /* fn */ function(txt)
{alert(txt);} );

but how can I pass a parameter to the bound function "fn" ?

Simply define the "fn" argument as an anonymous function that
specifies inside of it all the special parameters or initialization
that you want.

For instance, if your actual event handler takes both the expected
"evt" parameter along with an additional "foo" parameter, then the
"fn" argument could be this:

function (evt) { myhandler(evt, "bar"); }
 
M

Max

i have a event bind function like this(though it is not so robust):
bind$=function(o,evt,fn,cb){
var aE='attachEvent';
var aEL='addEventListener';
if(!o[aE]&&o[aEL]){
return o[aEL](evt,fn,!!cb);
}
return o[aE]('on'+evt,fn);

some usage like this:
bind$( /* o */ document, /* evt */ 'click', /* fn */ function(txt)
{alert(txt);} );
but how can I pass a parameter to the bound function "fn" ?

Simply define the "fn" argument as an anonymous function that
specifies inside of it all the special parameters or initialization
that you want.

For instance, if your actual event handler takes both the expected
"evt" parameter along with an additional "foo" parameter, then the
"fn" argument could be this:

function (evt) { myhandler(evt, "bar"); }

thanks for reply.

may be I didnt display my situation clearly.
my bind function goes :
under firefox it is supposed to be like

document.addEventListener( 'click', function(txt){alert(txt);} ,
false );
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
but I found problem when trying to transfer a parameter to the one
argument txt.
 
V

VK

i have a event bind function like this(though it is not so robust):
bind$=function(o,evt,fn,cb){
var aE='attachEvent';
var aEL='addEventListener';
if(!o[aE]&&o[aEL]){
return o[aEL](evt,fn,!!cb);
}
return o[aE]('on'+evt,fn);
};
some usage like this:
bind$( /* o */ document, /* evt */ 'click', /* fn */ function(txt)
{alert(txt);} );
but how can I pass a parameter to the bound function "fn" ?
Simply define the "fn" argument as an anonymous function that
specifies inside of it all the special parameters or initialization
that you want.
For instance, if your actual event handler takes both the expected
"evt" parameter along with an additional "foo" parameter, then the
"fn" argument could be this:
function (evt) { myhandler(evt, "bar"); }

thanks for reply.

may be I didnt display my situation clearly.
my bind function goes :
under firefox it is supposed to be like

document.addEventListener( 'click', function(txt){alert(txt);} ,
false );
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
but I found problem when trying to transfer a parameter to the one
argument txt.

On event listener call there are two "persistent" objects available:
1) the bound HTML element referred by "this"
2) the event handler referred by "arguments.callee"
The first one can be used to store element-specific extra info. There
is also Function constructor - this is what I am using most often -
and closures which is an awful way IMO to use for identical event
handlers.

-- Option 1 --

refHtmlElement.
addEventListener('click',
new Function(txt, functionBody),
false);

-- Option 2 --
refHtmlElement.
addEventListener('click',
myFunction,
false);
refHtmlElement.args = {
'txt' : txt
};

and then later

function myFunction(evt) {
var txt = this.args.txt;
// ...
}
 
M

Max

i have a event bind function like this(though it is not so robust):
bind$=function(o,evt,fn,cb){
var aE='attachEvent';
var aEL='addEventListener';
if(!o[aE]&&o[aEL]){
return o[aEL](evt,fn,!!cb);
}
return o[aE]('on'+evt,fn);
};
some usage like this:
bind$( /* o */ document, /* evt */ 'click', /* fn */ function(txt)
{alert(txt);} );
but how can I pass a parameter to the bound function "fn" ?
Simply define the "fn" argument as an anonymous function that
specifies inside of it all the special parameters or initialization
that you want.
For instance, if your actual event handler takes both the expected
"evt" parameter along with an additional "foo" parameter, then the
"fn" argument could be this:
function (evt) { myhandler(evt, "bar"); }
thanks for reply.
may be I didnt display my situation clearly.
my bind function goes :
under firefox it is supposed to be like
document.addEventListener( 'click', function(txt){alert(txt);} ,
false );
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
but I found problem when trying to transfer a parameter to the one
argument txt.

On event listener call there are two "persistent" objects available:
1) the bound HTML element referred by "this"
2) the event handler referred by "arguments.callee"
The first one can be used to store element-specific extra info. There
is also Function constructor - this is what I am using most often -
and closures which is an awful way IMO to use for identical event
handlers.

-- Option 1 --

refHtmlElement.
addEventListener('click',
new Function(txt, functionBody),
false);

-- Option 2 --
refHtmlElement.
addEventListener('click',
myFunction,
false);
refHtmlElement.args = {
'txt' : txt

};

and then later

function myFunction(evt) {
var txt = this.args.txt;
// ...

}


thanks VK
I was testing.....
but if the addEventListener is used in some class and the
refHtmlElement should be document, then the way in option2 you
mentioned just not work.
class like:
idle=function(callback){
this.starttime=new Date();
this.bound=false;
this.fn=callback;
this.reset=function(){
this.starttime=new Date();
};
this.init=function(){
if(!this.bound){
document.
addEventListener('mousemove',
this.reset,
false);
this.bound=true;
}
var secs=((new Date())-this.starttime)/1000;
if( secs % 59 == 0 ) {
this.fn();
}
}
....
};
....
/*
myidle=new idle(some function);
setInterval('myidle.init();',1000);
*/
i want to indicate (by mousemove event) if the user
left the page for every 60 seconds, and
the function fn is tiggered.
and by reset the starttime to new Date() while the user is
still active on the page.(also by mousemove event)
but after event bound, in the this.reset function,
this is no more refer to the object i create (eg. myidle),
~~~~
but refer to document instead.
I should use document for the "refHtmlElement", do you
think not?

any solutions?
 
R

RoLo

how about this?

var idle=function(callback){
var me=this;
        me.starttime=new Date();
        me.bound=false;
        me.fn=callback;
        me.reset=function(){
                me.starttime=new Date();
                };
        me.init=function(){
                if(!me.bound){
                        document.
                        addEventListener('mousemove',
                                me.reset,
                                false);
                        me.bound=true;
                }
                var secs=((new Date())-me.starttime)/1000;
                if( secs % 59 == 0 ) {
                        me.fn();
                }
        }
        ....};

....
/*
myidle=new idle(some function);
setInterval('myidle.init();',1000);
*/
 
M

Max

how about this?

var idle=function(callback){
var me=this;
me.starttime=new Date();
me.bound=false;
me.fn=callback;
me.reset=function(){
me.starttime=new Date();
};
me.init=function(){
if(!me.bound){
document.
addEventListener('mousemove',
me.reset,
false);
me.bound=true;
}
var secs=((new Date())-me.starttime)/1000;
if( secs % 59 == 0 ) {
me.fn();
}
}
....};

....
/*
myidle=new idle(some function);
setInterval('myidle.init();',1000);
*/

Yeah, it works very nicely.

Thank you all very much!
 

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,774
Messages
2,569,599
Members
45,175
Latest member
Vinay Kumar_ Nevatia
Top