Je veux compter combien de temps, je passe la souris sur un élément HTML.
pour eample
lien
Check me Out !
jQuery('.mylink').hover(function(){ //what should i do here to count });
Merci d’avance!
Si vous souhaitez conserver un compteur distinct pour chaque élément correspondant, procédez comme suit:
jquery('.mylink').mouseover(function(){ var $this = $(this); var count = parseInt($this.data('count'), 10) + 1; $this.data('count', count); });
Ensuite, vous pouvez obtenir le nombre pour chaque élément en utilisant $(selector).data('count')
.
Edit: Correction d’une erreur stupide.
$(function() { var myCounter = 0; $('.mylink').mouseover(function() { myCounter++; }); });
Si vous voulez savoir combien de secondes vous avez survolé sur un élément, essayez ceci:
$('.mylink').hover( //mouseover handler function(){ //record the current time $(this).data( 'start', new Date().getTime() ); }, //mouseout handler function(){ //grab the end time var end = new Date().getTime(); //calculate the difference in seconds var hoverTime = ( end - $(this).data('start') )/1000; //use the result alert( hoverTime.toFixed( 2 ) ); } );
appelez cette fonction …
jquery('.mylink').hover(function(){ start(true) }); function start(isStarted, index){ if(!index){ index = 0; } if(isStarted) { started[index] = true; counter[index] =0; } if(started[index] && true) { counter[index] += 1; timer = setTimeout("start(false," + index + ")",1000); } }