Ajax请求一旦在鼠标悬停时发送,就会收到响应,但是当我再次将鼠标悬停在HTML元素上时就不应该再发送了


ajax request once sent on mouseover, response recieved, but should not be sent again when i mouseover on the html element again

我正在发送一个ajax请求在鼠标事件,我正在接收所需的响应。但是,当我再次在发出请求的元素上发送hover时,请求将再次发送。

我不想再次发送请求,而是希望页面使用先前收到的响应。

谁来做??下面是一个示例代码。->此函数在鼠标悬停时被调用,不希望在相同的鼠标悬停元素上再次发送请求。
function showImage(val)
{
    $("#DIV").html('<IMAGESOURCE="../adsd/ajax_loader.gif">');
    $.ajax({
        type        : "get",
        cache   : false,
        url     : "blabla.php?imgID="+val,
        data        : $(this).serializeArray(),
        success: function(data) {
            document.getElementById("DIV").innerHTML = data;
        }
    });
};

在收到响应时设置一个变量值,并在发送请求之前检查该变量。

var response = false;
function showImage(val)
{
    $("#DIV").html('');
    if (response == false) {
        $.ajax({    
            type    : "get",
            cache   : false,
            url     : "blabla.php?imgID=" + val,
            data    : $(this).serializeArray(),
            success: function(data)
            {
                document.getElementById("DIV").innerHTML = data;
                response = true;
            }            
        });
    } else {
        // What to do if the request was sent before
    }
};

我认为你需要在鼠标悬停时使用jQuery.data()

if(!jQuery.data(dom, 'sent')){
  //take action
  jQuery.data(dom, 'sent', true);// do it on success
}