Add delay/timeout to a . Hover()

Asked

Viewed 63 times

1

Hello, I have a function. Where I would like that in the second instance (mouseleave) a delay occurs so that what is within the function occurs, I have already tried to set within the second function a setTimeout, I have tried to reference inside the . Hover, ". Hover(Function(){}, setTimeout(Function(){});), but still did not achieve an effective result. Function below:

    $("#lista-interface").on("mouseover", ".tarefas", function(){
        $(this).hover(function(){
            let divO = document.createElement("div");
            $(divO).attr({"class":("button" + " left"),});
            let divT = document.createElement("div");
            $(divT).attr({"class":("button" + " rigth"),});
            let divTR = document.createElement("div");
            $(divTR).attr({"class":("button" + " delete"),});
            $(this).append(divO);
            $(this).append(divT);
            $(this).append(divTR);
        }, function(){

            $("div").remove(".button, .left");
            $("div").remove(".button, .rigth");
            $("div").remove(".button, .delete");
            set
        });
    });

any idea how that could be an effect, or if there was some other way that I could achieve the same effect.

  • Because it does not use $("#lista-interface .tarefas").hover direct without having to use a mouseover before?

  • Because I was having conflicts with that function because the class . tasks only appear after the page is loaded completely, so I needed to link the over to an item that is already initially on the page so that when the event occurs there is no conflict

1 answer

1


Placing an event inside the other is not good practice because calling the first will create multiple listeners of the second. For example, when calling mouseover, will create multiple .hover(), that is, when you call the mouseover for the second time, will fire the .hover() 2 times, and so consecutively.

To resolve the delay issue, you include the code of the second function in the setTimeout:

setTimeout(function(){
   $("div").remove(".button, .left");
   $("div").remove(".button, .rigth");
   $("div").remove(".button, .delete");
}, 2000); // executa após 2 segundos

Regarding the problem described at the beginning, I would suggest not using .hover(). You can use the two events of .hover() so that it works even if the elements do not already have in the DOM, just use a if...else verifying which event was fired:

$(document).on("mouseenter mouseleave", "#lista-interface .tarefas", function(event){

     if(event.type == "mouseenter"){
         let divO = document.createElement("div");
         $(divO).attr({"class":("button" + " left"),});
         let divT = document.createElement("div");
         $(divT).attr({"class":("button" + " rigth"),});
         let divTR = document.createElement("div");
         $(divTR).attr({"class":("button" + " delete"),});
         $(this).append(divO);
         $(this).append(divT);
         $(this).append(divTR);
     }else if(event.type == "mouseleave"){
        setTimeout(function(){
            $("div").remove(".button, .left");
            $("div").remove(".button, .rigth");
            $("div").remove(".button, .delete");
        }, 2000);
     }
});
  • I did not know that I could use on() in this way, now a doubt of mine, Function(Event) when passed in this way the interpreter understands that what is being passed is an event? or if I put any name inside Function parentheses it would pass the same "value" for function.

  • You can put any name yes.

  • I understand checking the event in this way I ended up having another conflict see, this class . tasks is assigned to a div that contains two textareas, as I previously presented the mouse was over the textarea but still in the scope of the div the Buttons were displayed, however the way you gave me the delay starts to count even if the mouse is over the textarea, some idea of how I could solve this?

  • I’ll analyze it for you....

  • Look I took If and left only if in mouseleave and apparently it is working very well, I believe it can be conflicts with other functions of mine because I am new with js and jquery, so my code is a mess so it may be something here with myself.

  • If it’s working, blz... if it’s a problem, call me here.

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.