Solution 1 :

This should work, although I could not test it as you are not allowed to set a cookie on a third-party website.

<!DOCTYPE html>
<html>

<head>
  <title>Page Title</title>
</head>

<body>
  <button id="refresh" type="button" data-toggle="tooltip" data-placement="bottom" class="btn-shadow mr-3 btn btn-dark">Refresh</button>

  <script>
    const setRefreshCookie = () => {
      const now = new Date();
      const tomorrow = new Date();
      /*
       * if you want the cookie to expire after 24 hours from the last click
       * on the refresh button, you use only 'tomorrow.setTime()' otherwise,
       * if you want the cookie to expire as soon as the date changes (even if
       * 24 hours have not passed yet) you use both 'tomorrow.setTime()' and
       * 'tomorrow.setHours()'
       */
      tomorrow.setTime(now.getTime() + 1000 * 60 * 60 * 24);
      tomorrow.setHours(0, 0, 0, 0);

      document.cookie = 'preventRefesh=true;expires=' + tomorrow.toUTCString() + 'domain=yourdomain.tld;path=/path/to/page';
    };

    const checkRefreshCookie = () => {
      return document.cookie.split('; ').some(cookie => cookie.split('=')[0] === 'preventRefesh');
    };
    
    const refreshBtn = document.getElementById('refresh');
    const isRefreshCookieSet = checkRefreshCookie();

    document.addEventListener('load', () => {
      if (isRefreshCookieSet ) {
        /*
         * disable the button, so that the user has a visual feedback
         */
        refreshBtn.setAttribute('disabled', true);
      }
    });

    refreshBtn.addEventListener('click', (event) => {
      if (!isRefreshCookieSet ) {
        setRefreshCookie();
        // location.reload();
        console.log('refeshing the page...');
      }
      event.preventDefault();
      event.stopPropagation();
    });
  </script>
</body>

</html>

Problem :

I have a code that will refresh the page on button click , but when i give only to refresh once in a day , it’s not allowing me to refresh . and sometimes when i load the page it will not allow me to refresh the page .

function CheckRefresh(){
  var lastRefresh = new Date(); 
  setInterval(function(){
       var now = new Date();
       if (new Date() - lastRefresh > 1000 * 60 * 60 * 24) { 
                  location.reload();
       }

   },60000);
                                           //Console.log(lastrefresh);
}
<button type="button" data-toggle="tooltip" onClick="CheckRefresh()" data-placement="bottom" class="btn-shadow mr-3 btn btn-dark">Refresh</button>

I had already referred post related to this on StackOverflow .

Comments

Comment posted by davidgiesemann

You need some kind of persistent storage to do this. You can do it using a cookie – if no cookie is set OR refreshtime in cookie > 24h: allow the refresh

Comment posted by sreesha sudhev

yeah it works fine , but when i refresh my browser refresh button everything start’s from first that is refresh button will be enabled again , is there any solution for that . it would be a great help .

Comment posted by sreesha sudhev

i want in such a way that whatever if someone refresh using button then the button should be disabled throughout the day even if we refresh the browser or reloads the page

Comment posted by davidgiesemann

@sreeshasudhev You cannot prevent a native refresh event.

Comment posted by secan

@sreeshasudhev, you can move the logic to disable the page button from the button click event to the document load event (I updated the snippet in order to do so) but you cannot – and you should not -prevent the user from refreshing the page using the browser toolbar button.

Comment posted by sreesha sudhev

but if it is deployed in the server and many user loads the webpage . refresh button will be enabled na

By