True speaker
True speaker

Reputation: 362

How to disable this button for 5 sec after first click?

In my code the button .getmore gets clicked on scroll down. It loads data from database.Works fine. I need to make it disable for 5 sec after every click. How can I set this delay timer?

Or is it possible to disable this function for 5 sec once it fires? Disable the button or disable the function anything will work for me.

$(window).scroll(function(){
    if ($(document).height() <= $(window).scrollTop() + $(window).height()) {
        // Here how can i set a delay time of 5 sec before next click 
        $('.getmore').click(); 
    }
});

Upvotes: 4

Views: 10123

Answers (4)

maioman
maioman

Reputation: 18762

You could use a flag inside the clickHandler:

var clickHandler = () => {
  if (clickHandler.running)
    return
  
  clickHandler.running = true
  setTimeout(() => { clickHandler.running = false }, 5000)
  
  console.log(new Date())
  }

$('#getmore').click(clickHandler)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id='getmore'>more</div>

Upvotes: 1

Garry14
Garry14

Reputation: 41

U can use setTimeout() n after that, put ur disable button function inside it.

setTimeout(document.getElementById('ur_button_id').style.display = 'none', 500);

Upvotes: -1

Nishu Tayal
Nishu Tayal

Reputation: 20880

You can use setTimeout() function of jQuery.

 $('.getmore').prop('disabled', true);
 setTimeout(function() {
       $('.getmore').prop('disabled', false);
 }, 5000);

Upvotes: 7

Jeoff
Jeoff

Reputation: 99

One solution is to use setTimout and disable the button with javascript using document.getElementById("yourButtonID").disabled = true;

Upvotes: 1

Related Questions