Search code examples
htmltimeoutpassword-protection

Timeout function for new tab


I have password protected a link that will open in a new tab but i now want that tab to timeout after a short while. I have tried this but to no avail, any help would be amazing!

<script>

var newWin;

function validatePass(){
    if(document.getElementById('password').value == 'Techsoc'){
       newWin = window.open('https://docs.google.com/spreadsheets/d/e/2PACX-1vRsNGb741vTBtSGlzbxnTsoZpFMs0l9ZMrD54uE4XGy6FvSMmNfp3RPWvDU3vI32iSIp07CEshGLo0M/pubhtml');
           }else{
        alert('Incorrect Password, please try again.');
        return false;
setTimeout(function(){newWin.close()}, 5000);
        }
}
</script>


Solution

  • The issue was with the timeout being within the else statement, when the new window have been created within the if. Now if you type in the password and click the button it should open the new tab and close after the time set in the timeout.

    <input type="password" id="password"></input>
    <button OnClick="validatePass()">Log In</button>
    <script>
    
        var newWin;
    
        function validatePass() {
            if (document.getElementById('password').value == 'Techsoc') {
                newWin = window.open('https://docs.google.com/spreadsheets/d/e/2PACX-1vRsNGb741vTBtSGlzbxnTsoZpFMs0l9ZMrD54uE4XGy6FvSMmNfp3RPWvDU3vI32iSIp07CEshGLo0M/pubhtml');
                setTimeout(
                    function () {
                        newWin.close()
                    }, 5000);
            }
            else {
                alert('Incorrect Password, please try again.');
                return false;
            }
        }
    </script>