18910140161

html-如何防止在Javascript中已经启动的“onclick”功能,即使在重新加载页面之后也是如此?-堆栈溢出

顺晟科技

2022-10-19 12:04:56

215

我有一个带有“onclick”功能的按钮,可以禁用该按钮15秒。之后,它将自动被点击,但是在点击后的15秒之间,如果我刷新了页面,它将不计算剩余的秒数,按钮现在可以被点击了。

function up(){
    $(document).ready(function () {
          $("#upside").attr("disabled", true);
          document.getElementById("downside").disabled = true;
            setTimeout(function () {
              $("#upside").removeAttr("disabled");
              $("#downside").removeAttr("disabled");
              window.location ='/Balnce_add';
                  },15000);
  });
    }

下面是我的HTML按钮代码:

function up(){
    $(document).ready(function () {
          $("#upside").attr("disabled", true);
          document.getElementById("downside").disabled = true;
            setTimeout(function () {
              $("#upside").removeAttr("disabled");
              $("#downside").removeAttr("disabled");
              window.location ='/Balnce_add';
                  },15000);
  });
    }

顺晟科技:

这个想法是保存到。然后当我们重新加载页面时,我们将得到。如果,我们将保留禁用按钮。如果没有,我们什么也不做。

基于这个想法,我更新了您的代码以使其正常工作。您可以通过下面的代码检查演示:

function up(){
    $(document).ready(function () {
          $("#upside").attr("disabled", true);
          document.getElementById("downside").disabled = true;
            setTimeout(function () {
              $("#upside").removeAttr("disabled");
              $("#downside").removeAttr("disabled");
              window.location ='/Balnce_add';
                  },15000);
  });
    }

此外,您可以在https://codepen.io/tuandaodev/pen/nwgbjbv

查看现场演示

这取决于应用程序是如何设置的,但一种简单的方法是在localhost中存储点击按钮的ID。

我写得很快,所以可能有点粗糙,但一般的想法是,当按钮被单击时,将一些内容添加到localstorage中,并在页面加载上检查localstorage,如果按钮被单击后还没有15秒,则禁用它

  • TAG:
相关文章
我们已经准备好了,你呢?
2024我们与您携手共赢,为您的企业形象保驾护航