禁用按钮一段时间

时间:2020-04-04 00:26:44

标签: javascript html

用户按下发送按钮后,我希望禁用发送按钮10秒钟。 10秒过后,应再次启用该按钮。

setTimeout(resetMaterialTextfield(messageInputElement), 10000);函数在下面似乎无法使用。

function onMessageFormSubmit(e) {
  e.preventDefault();

  // Check that the user entered a message that is a number between 1 and 100 and is signed in.
  if (messageInputElement.value && checkSignedInWithMessage()) {
    if (isNaN(messageInputElement.value)){
      window.alert(" Please enter a number between 1 - 100 (inclusive) ");
    }
    else {
      if (messageInputElement.value <= 0 || messageInputElement.value > 100){
        window.alert(" Please enter a number between 1 - 100 (inclusive) ");
      }
      else {
        saveMessage(messageInputElement.value).then(function () {
          // Clear message text field and re-enable the SEND button.
          setTimeout(resetMaterialTextfield(messageInputElement), 1000);
          toggleButton();
        });
      }
    }
  }
}

这是另一个功能:

function toggleButton() {
  if (messageInputElement.value) {
    submitButtonElement.removeAttribute('disabled');
  } else {
    submitButtonElement.setAttribute('disabled', 'true');
  }
}

请为您提供帮助。

谢谢!

1 个答案:

答案 0 :(得分:-1)

尝试setTimeout(resetMaterialTextfield.bind(null, messageInputElement), 1000);