How to rate-limit clicks on a button to once per minute in Javascript

前端 未结 5 570
后悔当初
后悔当初 2021-01-24 05:33

I have a PHP-based web application that monitors the status of a process and displays a page with that status. Users can click a button on the page to update the status. However

5条回答
  •  后悔当初
    2021-01-24 06:09

    Just a suggestion, but if you're going to go through the trouble to write the code to rate-limit the submit button by using a client-side timer (setTimeout), why not just remove the update button all together, and make the page auto-update at your pre-defined interval?

    Here's some sample code in jQuery (since jQuery offers some great cross-browser AJAX support)

    
    
        
        
        $(function(){
            // On page load...
            updateStatus();
        })
    
        function updateStatus(){
            $('#status').load('/url/to/status/display.php');
            setTimeout(updateStatus, 60000) // 60,000 miliseconds = 1 minute
        }
        
    
    
    
        

提交回复
热议问题