简体   繁体   中英

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(); 
    }
});

You can use setTimeout() function of jQuery.

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

一种解决方案是使用setTimout并使用document.getElementById("yourButtonID").disabled = true; 禁用具有javascript的按钮 document.getElementById("yourButtonID").disabled = true;

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

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

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> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM