jQuery setTimeout

前端 未结 6 2351
刺人心
刺人心 2021-02-15 11:32

I\'d like to add a timeout to this tooltip code so it only displays if the mouse hovers over it after a while rather than immediately... I tried adding the setTimeout()

6条回答
  •  北荒
    北荒 (楼主)
    2021-02-15 12:16

    var my_timer;
    $('.mcTxb').hover(
        function () {
            my_timer = setTimeout(function () {
                //do work here
            }, 500);
        },
        function () {
            clearTimeout(my_timer);
        }
    );
    

    This will wait half a second before doing whatever when you mouseover the element and the whatever will not happen if you mouseout within the half second.

提交回复
热议问题