var buttonHovered = false; $('#button').hover(function () { buttonHovered = true; while (buttonHovered) { ... } }, function () { buttonHovered = false; });
If you want to do this for multiple objects, it might be better to make it more object oriented than a global variable.
Edit: Think that the best way to deal with multiple objects is to put it in a .each () block:
$('myselector').each(function () { var hovered = false; $(this).hover(function () { hovered = true; while (hovered) { ... } }, function () { hovered = false; }); });
Edit2 : Or you can do this by adding a class:
$('selector').hover(function () { $(this).addClass('hovered'); while ($(this).hasClass('hovered')) { ... } }, function () { $(this).removeClass('hovered'); });
source share