javascript会在一段时间后启用已禁用的文本框

时间:2016-06-02 13:12:10

标签: javascript jquery ajax

我一直在尝试各种各样的事情并寻找答案。我找到了一些但是因为一些奇怪的原因它们不起作用。

我正在使用txt文档存储数组并使用ajax从中获取GET。 它可以在3次尝试失败后禁用这些字段,但是如何使其在10秒后再次启用呢?

这是我的javascript代码:

  import javax.swing.JFrame;
  import javax.swing.JLabel;
  import java.awt.FlowLayout;

  public class MainProgram{

  public static void main(String[] args) {
    JFrame frame = new JFrame("This is the title of the window");//adding the JFrame or window
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//so it really and literally close when we 
    //hit the close button on the window
    frame.setVisible(true);//setting the visibility
    //adding the label
    JLabel label_1 = new JLabel("this is a JLabel");
    //adding the label to the window
    label_1.setToolTipText("This is the tool tip");
    add(label_1);
}
}

else {
    attempt--; // Decrementing by one.
    alert("You have left " + attempt + " attempt;");
    document.getElementById("password").value = '';
    // Disabling fields after 3 attempts.
    if (attempt == 0) {
        function disablePsw() {
            document.getElementById("username").disabled = true, 5000;
            document.getElementById("password").disabled = true, 5000;
            document.getElementById("submit").disabled = true, 5000;
            return true;
        }
    }
}

3 个答案:

答案 0 :(得分:1)

试试这个:

在给定时间后使用setTimeout()启用按钮:

function disablePsw() {
    document.getElementById("username").disabled = true;
    document.getElementById("password").disabled = true;
    document.getElementById("submit").disabled = true;

    setTimeout(function() { // it will automatically execute and enable all button after 10 seconds
        document.getElementById("username").disabled = false;
        document.getElementById("password").disabled = false;
        document.getElementById("submit").disabled = false;
    }, 10000); 

    return true;
}

答案 1 :(得分:0)

您可以在外部定义该功能,然后将其调用为禁用。在setTimeout之后再次启用。

else {
    attempt--; // Decrementing by one.
    alert("You have left " + attempt + " attempt;");
    document.getElementById("password").value = '';
    // Disabling fields after 3 attempts.
    if (attempt == 0) {
        togglePsw(true);
        setTimeout(function(){
            togglePsw(false)
        }, 5000)
    }
}

function togglePsw(c) {
    document.getElementById("username").disabled = c;
    document.getElementById("password").disabled = c;
    document.getElementById("submit").disabled = c;
    return true;
}

答案 2 :(得分:0)

setTimeout(function(){
    $("#username").prop('disabled', false);
    $("#password").prop('disabled', false);
    $("#submit").prop('disabled', false);
},10000);