JQuery-handle the latency of a hover event

Source: Internet
Author: User

JQuery-handle the latency of a hover event with the mouse. The specific JS Code is as follows:

(function($){    $.fn.hoverDelay = function(options){        var defaults = {            hoverDuring: 200,            outDuring: 200,            hoverEvent: function(){                $.noop();            },            outEvent: function(){                $.noop();            }        };        var sets = $.extend(defaults,options || {});        var hoverTimer, outTimer;        return $(this).each(function(){            $(this).hover(function(){                clearTimeout(outTimer);                hoverTimer = setTimeout(sets.hoverEvent, sets.hoverDuring);            },function(){                clearTimeout(hoverTimer);                outTimer = setTimeout(sets.outEvent, sets.outDuring);            });        });    }})(jQuery);

The hoverDelay method has four parameters, which indicate the following:

Delay Time of hoverDuring mouse
Latency of outDuring mouse Removal
How to execute hoverEvent with the mouse
OutEvent mouse Removal Method

The purpose of this function is to let the mouse go through the separation of events and latencies, and to clear the latencies and latencies. All you need to do is set the delay time and the corresponding mouse to go through or remove the event. The following code is a simple example:

$("#test").hoverDelay({hoverDuring: 1000,outDuring: 1000,hoverEvent: function(){$("#tm").show();},outEvent: function(){$("#tm").hide();}});

The following is a more concise case:

$ ("# Test"). hoverDelay ({hoverEvent: function () {alert ("Pass me! ");}});

It indicates that the element with the id of test is displayed after the mouse passes through 200 milliseconds and contains "pass by me !" Text dialog box.

Articles you may be interested in
  • Js shields mouse and keyboard events (including right-click, direction key, backspace key, F5 refresh key, etc.), compatible with IE and firefox
  • Jquery simulates Mouse clicking events
  • Solution to failure of jquery live change event under IE
  • Js: Click enter and move the cursor to the next input box.
  • Escape user-passed variables, from ecshop
  • How to Prevent event bubbling in JQuery and Its Differences
  • How does js determine whether the scroll wheel is scroll down or up?
  • A classic dialogue between programmers and testers. These are shared by foreign programmers, who say they are used globally?

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.