Is it possible to somehow force the function to start if the user does not move the mouse for 5 seconds? There is a hidden div with animation, it is necessary that it does not appear immediately when the page is loaded, or not just after 5 seconds, namely when the mouse cursor is 5 seconds long.

At the same time, if he moves the mouse, then the timer is discarded and starts counting only when the cursor again stops.

  • four
    Of course you can! :) - iKuzko
  • one
    As an option to make a timer and when the rodent is active, reset it + check for a timer as soon as it exceeds the threshold value to show a divnik. I myself did not do anything like that in a pure fantasy. - zenith
  • Yes, you yourself know everything :) What are you asking stupid questions for? - zenith
  • zenith, don't be nervous - new_user_1
  • @ new_user_1 Calm like an elephant. Marble - zenith 10:06 pm

3 answers 3

$('#hideDiv').hide(); // прячим Π½Π΅ΠΎΠ±Ρ…ΠΎΠ΄ΠΈΠΌΡ‹ΠΉ Π΄ΠΈΠ² var showDiv = function(){ $('#hideDiv').show(); }; // функция для Π΅Π³ΠΎ отобраТСния, Π±ΡƒΠ΄Π΅Ρ‚ ΡΡ‚ΠΎΡΡ‚ΡŒ Π½Π° Ρ‚Π°ΠΉΠΌΠ΅Ρ€Π΅ var timeOut = setTimeout(showDiv, 3000); // ΠΎΠΆΠΈΠ΄Π°Π½ΠΈΠ΅ показывания Π΄ΠΈΠ²Π° $('body').mousemove(function(){ clearTimeout(timeOut); // Ссли Π΄Ρ‘Ρ€Π½ΡƒΠ»ΠΈ ΠΌΡ‹ΡˆΠΊΠΎΠΉ ΠΎΠ±Π½ΡƒΠ»ΠΈΠ»ΠΈ ΠΎΠΆΠΈΠ΄Π°Π½ΠΈΠ΅ $('#hideDiv').hide(); // спрятали Π΄ΠΈΠ² timeOut = setTimeout(showDiv, 3000); // Π½Π°Ρ‡Π°Π»ΠΈ ΠΆΠ΄Π°Ρ‚ΡŒ снова }); 

proof

UPD: update from comrade. @neoascetic

 function showHidenBlock(){ this.timeout = null; this.showBlock = function(){ $('#myHidenBlock').show(); }; this.resetTimeout = function(){ clearTimeout(this.timeout); this.timeout = setTimeout(this.showBlock, 5000); } } var showHidenBlock = new showHidenBlock(); $(window).on({ mousemove:function(){showHidenBlock.resetTimeout()} }); 

As a solution. But next time it is advisable to ask a question more accurately :)

    Here is a handy jquery plugin for your purpose: jquery-idletimer-plugin

    DEMO