最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How detect any of all events using jquery? - Stack Overflow

programmeradmin4浏览0评论

I have function to detect idle state of user. I want to update database if any of the event occurs.Now i have script like this

$("body").mousemove(function(event) {
myfuction();
});

I want to convert above script like this

$("body").anyOfTheEvent(function(event) {
myfuction();
});

How can i do this?

I have function to detect idle state of user. I want to update database if any of the event occurs.Now i have script like this

$("body").mousemove(function(event) {
myfuction();
});

I want to convert above script like this

$("body").anyOfTheEvent(function(event) {
myfuction();
});

How can i do this?

Share Improve this question asked Aug 8, 2012 at 9:41 EbinPauloseEbinPaulose 1,1393 gold badges14 silver badges26 bronze badges 3
  • 3 Why do you need to detect user's idle state? It may be you're having a design flaw in your application because this may put your web application to crawl. Expecially if you'll wire Ajax requests to your events (mentioning DB). It will surely make it pletely unusable. Believe me. All event's transferred to DB will surely halt it. A simple mouse move happens much more frequently than you can wire these events to the server and store them in the database. – Robert Koritnik Commented Aug 8, 2012 at 9:44
  • I have a time limit in "Myfuction".only one Ajax request occur per 2 minute – EbinPaulose Commented Aug 8, 2012 at 9:49
  • Still: Why do you need to detect user idle state? – Robert Koritnik Commented Aug 8, 2012 at 9:52
Add a ment  | 

5 Answers 5

Reset to default 2

You can find the event name using the e.type property. Try looking this example

  $('#element').bind('click dblclick mousedown mouseenter mouseleave',
                     function(e){
                         alert("EventName:"+e.type);
                     });
    ​

The jsfiddle for this is here http://jsfiddle/qp2PP/

You could have an array of the events you're interested in and subscribe to all of them

var events = ['click','mousemove','keydown'] // etc
$.each(events,function(i,e){
    $('body')[e](myfuction);    
});

Get a list of events here: http://api.jquery./category/events/

You can bind more than one event with bind()

$('#foo').bind('click mousemove', function(evt) {
    console.log(evt.type);
});

just use on(), with a space-delimited list of events:

$('body').on('mousedown click', function(e) {
    var  eventType = e.type;
    // do stuff
});

References:

  • on().

Instead of bind to specific element, you can bind directly to document.

$(document).bind('click mousemove', function(evt)       {
    document.getElementById("demo").innerHTML = Math.random();
});

Example in CODEPEN

发布评论

评论列表(0)

  1. 暂无评论