Javascript jQuery.remove()和setTimeout交互

Javascript jQuery.remove()和setTimeout交互,javascript,jquery,Javascript,Jquery,我有一个jQuery函数,当用户将鼠标悬停在某个元素上时,它会创建一个元素。当鼠标离开元素时,我设置了一个超时来销毁它。虽然创建和正常销毁元素的工作没有问题,但超时内的删除操作不起作用,并且会悄无声息地消失。我怀疑window.setTimeout和jQuery.remove之间的交互有问题,但在web上找不到任何内容 我的代码: //Element creation function createElement(){ $(".my_class").remove() conten

我有一个jQuery函数,当用户将鼠标悬停在某个元素上时,它会创建一个元素。当鼠标离开元素时,我设置了一个超时来销毁它。虽然创建和正常销毁元素的工作没有问题,但超时内的
删除操作不起作用,并且会悄无声息地消失。我怀疑
window.setTimeout
jQuery.remove
之间的交互有问题,但在web上找不到任何内容

我的代码:

//Element creation
function createElement(){
    $(".my_class").remove()
    content = compose_content() //It's an <ul>
    $('<div></div>', {
        class : "my_class",
        html: content
     }).insertAfter($("#first_row"))
}

//Element destruction
function setDestroyTimeout(){
    if(perform_some_checks()){
         window.setTimeout(function(){
              console.log("Removing...")
              $(".my_class").remove()
         }, 1000)
    }
}

$(".another_class").hover(createElement(), setDestroyTimeout())
//元素创建
函数createElement(){
$(“.my_class”).remove()
content=compose\u content()//这是一个
    $('', { 班级:“我的班级”, html:内容 }).insertAfter($(“#第一行”)) } //元素破坏 函数setDestroyTimeout(){ 如果(执行一些检查()){ setTimeout(函数(){ console.log(“删除…”) $(“.my_class”).remove() }, 1000) } } $(“.other_class”).hover(createElement(),setDestroyTimeout())

第一个移除(在
createElement
函数中的移除)工作顺利,而第二个移除没有任何作用。超时也能正常工作,我用
控制台.log
指令进行了检查。

调用vs reference
$(“.other_class”)。悬停(createElement(),setDestroyTimeout())
=>
$(“.other_class”)。悬停(createElement,setDestroyTimeout)
您能展开它吗?在
setDestroyTimeout()
中调用函数
setDestroyTimeout
有点不清晰。然后,该函数调用的结果(在您的示例中为
未定义的
)作为回调传递给您的
悬停
。看看这个相关的问题:当您设置第二个destroy时,元素可能还没有创建,jquery试图删除,但元素还没有添加。问题是@t.niese(我也怀疑@Tushar)指出的问题。谢谢