There is a block by which a click event can occur. How can you check for the absence of this event within 5 seconds. That is, if no one has clicked on the block for 5 seconds, deduce alert() .
Can this be done with jquery ?
- oneStart the timer, if the click was, then restart. Well, the word "absence" correct ... - qzavyer
- the absence of this event for 5 seconds starting from which event? - Grundy
- @Grundy since last click. If the user for 5 seconds does not click alert. And if it clicks, the check is postponed for 5 seconds. The test itself starts on a timer every 5 seconds - Dementiy1999
|
2 answers
var timeout = null; $('div').click(function(e) { clearTimeout(timeout); timeout = setTimeout(function() { alert('CLICK ME!!!'); }, 5000); }); <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div>Click me!</div> |
To wait for a click from the moment the document is loaded:
$(function(){ var $block = $('.block'); // нахожу нужный блок var $clicked = 0; // обнуляю статус нажатия на блок $(window).click(function(event) { // слушаю нажатие куда-либо if ( event.target.className == $block.attr('class') ) { // если нажатие на искомый блок $clicked = 1; // меняю статус нажатой переменной console.log('clicked'); } }) setTimeout(function() { // запускаю функцию, которая через 5 секунд выведет alert в случае нажатия на блок if ($clicked == 1) { alert('detected'); console.log('detected'); } else { console.log('not detected'); } }, 5000); }) .block { width: 100px; height: 100px; background: #000; } <script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.1/jquery.min.js"></script> <div class="block"></div> |