Not remove prop disabled with window.clearTimeout(timer);

After 5 seconds do not want remove disabled from button why?

var timer = setTimeout(disableMyButton, limit);


      $(document).ready(function() {
        var limit = 5; // the time limit (in milliseconds) for the disable function
        var timer = setTimeout(disableMyButton, limit);

        function disableMyButton() {
          $('#myButton').prop('disabled', true);
          $("#statusMsg").text("Disabling before page refresh");
          localStorage.setItem("isMyButtonDisabled", "true");

        if (localStorage.getItem("isMyButtonDisabled") == "true") {
          $("#statusMsg").text("Disabling after page refresh");
          $('#myButton').prop('disabled', true);


limit is 5 milliseconds … so basically instantly, guessing this is for testing :slight_smile:

  1. What happens in the first line when disableMyButton is called?

  2. window.clearTimeout(timer); … When does this happen in reference to timer and the callback?

Well is disabled button both way.

And save in localStorage the button is disabled so after refresh is will be also disabled. Now how about remove from localStorage after 5 minutes? There have a way?


What is your goal?

Right now, you are starting a setTimeout and then clearing it. disableMyButton never runs.

If you can explain what you want to happen and where you think you are having problems, we can help.

Lets change from 5 to 30 seconds.

So I want to save the button is disabled is works fine. But after reload still show that untill the 30 seconds is left? So then remove disable button (after 30 seconds) and then able click again on button. Based on localStorage not on page refresh the disable time.


:thinking: I’ve never attempted storing values during a reload orlocalStorage myself… so others will be able to help much more than I can.

I would assume that timer needs to be global and ignore a browser Refresh but again, not sure…

Why not just restart timer on each refresh?

1 Like

That is also a good idea refresh but see I have something like this

This disable button but I no idea how to set like for 30 second to it?

                myDate.setDate(myDate.getDate() + 1);

So this 1 value for current date. And if left 1 then able to submit again? I think?

What is 1? I think one day.