Reputation: 10230
Here is my code:
var e = jQuery.Event( "keydown", { keyCode: 64 } );
There is data being attached to the event in the second parameter. Now I have seem that kind of syntax in a lot of plugins and was wondering, what's the use of attaching arbitrary data to a $.event/custom event?
I have seen the jQuery docs event object and also trigger.
I am wondering, if the only usage of attaching data to a event is as follows:
var e = jQuery.Event( "keydown", { keyCode: 64 } );
// above is the line I am having difficulty understanding
// the usage of arbitrary data with the $.event is quite
// elusive to a novice developer like me
$(window).on('keydown' , function(e){
console.log('key 64 pressed');
});
press = function(){
$(window).trigger(e);
}
setTimeout(function(){
press();
}, 2000);
I.e. triggering an event on a specific key or a specific element, I mean is this the only use of attaching arbitrary data to an $.event
?
Upvotes: 0
Views: 51
Reputation: 8170
Sometimes you are using your code that behaves in some way depending on the interaction.
In your example, you are "manually" triggering the keydown
event on that input in order to "trigger" the event-listeners
listening to this event.
Probably on this example you have a idle logout system going on and you activated something to keep the session alive on your UI
as long as needed.
The other day I had to use something similar in order to keep a dropdown open in a third party plugin that was closing a drop down on selection after a search.
Upvotes: 1