I have a piece of code the creates a hovercard (a box with some additional info) on a social network type of site, whenever a user hovers with their mouse over some hyperlink that contains a username:
$(function() {
$('body').on('mouseenter', 'a', function(){
$(this).attr('data-value', '10');
let username = $(this).attr('href');
$.post("includes/handlers/hovercard.php", {username:username}, function(data){
if(data === "yes")
$('a[data-value=10]').wrap("<div class='hov' style='display:inline-block;'></div>");
else
$('a[data-value=10]').removeAttr('data-value');
});
$.post("includes/handlers/hovercard_response.php", {username:username}, function(response){
$('.hov').append('<span></span>');
$(".hov span").html(response);
$('.hov').each(function(){
$(this).on('mouseover', 'a', function(){
return false;
});
});
//...and then some buttons, links and etc are displayed...
}).on('mouseleave', 'a', function(){
$('a[data-value=10]').removeAttr('data-value');
});
});
The first ajax response checks if the user is hovering over a link that contains a username, and if so it sends "yes" as the response. If it's a link with a username, then the second call applies the hovercard with all the other stuff that should be displayed (I left that bit out not to clutter the post since it's irrelevant to the problem).
This all works fine. The only problem is that, occasionally, if you hover over the user A, for example, and then over the users B, C, and D it doesn't show the info for A, B, C, and D but A, A, A, A. In other words, it gets stuck and shows the info for only one user, as if you keep hovering over the same link over and over again.
I checked out the console and saw that when this happens, it does not send ajax requests at all. So instead of sending the request for B, it doesn't send anything but just keeps displaying A instead. If I start scrolling the page a bit, or refresh, the problem disappears and it's all normal again. It happens on both Chrome and Firefox.
I first thought it might be some caching issue, so I added $ajaxSetup({ cache:false })
at the very top, but that didn't help.
Any help is much appreciated...