【问题标题】:I have a js script which should disable a button but it's not working我有一个 js 脚本,它应该禁用一个按钮,但它不起作用
【发布时间】:2021-01-09 23:35:26
【问题描述】:

我有一个按钮可以让图像旋转 3 秒。我希望在旋转期间禁用该按钮。但这行不通。下面是创建旋转的 js 函数和 css 类。

   <script>
    function spin() {
      document.getElementById("spin_switch").disabled = true;
      document.getElementById("image-map").classList.toggle("spin");
      setTimeout(stop_spin, 3000);
      document.getElementById("spin_switch").disabled = false;
    }
    function stop_spin() {
      document.getElementById("image-map").classList.toggle("spin");
    }
   </script>
    
   <style>
    .spin {
      transform: rotate(360deg);
      webkit-transform: rotate(360deg);
      overflow: hidden;
      transition-duration: 3s;
      transition-property: transform;
    }
   </style>

【问题讨论】:

标签: javascript css button


【解决方案1】:

你必须移动这条线

document.getElementById("spin_switch").disabled = false;

进入stop_spin函数:

function spin() {
   document.getElementById("spin_switch").disabled = true;
   document.getElementById("image-map").classList.toggle("spin");
   setTimeout(stop_spin, 3000);
}
function stop_spin() {
   document.getElementById("image-map").classList.toggle("spin");
   document.getElementById("spin_switch").disabled = false;
}

否则 spin_switch 将再次立即启用。 setTimeout 不会停止整个脚本。它只是在给定的超时时间到期后注册一个要执行的回调。

【讨论】:

  • 谢谢。我认为 document.getElementById("spin_switch").disabled = false 行直到 3s setTimeout 函数完成后才会运行。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-10-17
  • 1970-01-01
  • 1970-01-01
  • 2021-09-17
相关资源
最近更新 更多