php - Function runs immediately without setTimeout() in Javascript

Solution:

This function will be invoked immediately. IIFE

(function(){
document.getElementById('nextbtn').disabled = false;
document.getElementById('nextbtn').onclick = function() {
window.open('https://www.youtube.com');
})()

Answer

Solution:

Is removing the setTimeout only not enough?

<script>
 (function(){
   document.getElementById('nextbtn').disabled = false;
   document.getElementById('nextbtn').onclick = function() {
   window.open('https://www.youtube.com');
 })();
</script>

Answer

Solution:

<script>
   document.getElementById('nextbtn').disabled = false;
   document.getElementById('nextbtn').onclick = function() {
      window.open('https://www.youtube.com');
   };
</script>

//---

<html>
<head>
    <title>HTML Document</title>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.min.js"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js"></script>
    <script>
        function countWord() {
            var x = document.getElementById("code").value;
            if ((x.length) != 12) {
                document.getElementById("ok").disabled = true;
            } else if ((x.length) == 12) {
                document.getElementById("ok").disabled = false;
            }
        }
    </script>
    <style>
        * {
            padding:0;
            margin:0;
            box-sizing:border-box;
        }
        body {
            background-color:#fff;
        }
        #code {
            width:98%;
            margin-left:1%;
        }
        #ok {
            width:50%;
            margin-left:25%;
            margin-top:10px;
        }
        #nextbtn {
            width:50%;
            margin-left:25%;
            margin-top:10px;
        }
    </style>
</head>
<body>
    <form method="POST" name="code_form" id="code_form">
        <input type="text" class="form-control" name="code" id="code" onkeyup="countWord();">
        <input type="submit" name="ok" id="ok" class="btn btn-primary" disabled>
    </form>
    <button type="button" disabled class="btn btn-primary" id="nextbtn">Next</button>

    <?php
    //$conn = mysqli_connect("", "", "", "");
    if (isset($_POST["ok"])) {
        $code = $_POST["code"];
        //$sql = "SELECT * FROM code WHERE code='$code'";
        //$result = mysqli_query($conn, $sql);
        //if (mysqli_num_rows($result) == 1) {
        if (1 == 1) {
            echo "<script>
        document.getElementById('nextbtn').disabled = false;
        document.getElementById('nextbtn').onclick = function() {
            window.open('https://www.youtube.com');
        };
    </script>";
        } else {
            echo "<script>alert('Error');</script>";
        }
    }
    ?>
</body>

Source