-1

I cam across this code that was like a login timer e.g. if the login credentials were wrong, a timer of 5 seconds would be appear till next login and then if you fail again it would go up to like 15. Would you guys know how it's done?

Thank you.

.......( document ).ready( function() {
    (function(){
    var lTimeoutField = document.getElementById("throttle_sec"),
        lTimeout      = lTimeoutField ? +lTimeoutField.innerHTML : 0;
    if (lTimeout) {
        var lTimer = window.setInterval (
            function() {
                if (lTimeout > 0) {
                    lTimeoutField.innerHTML = lTimeout;
                    lTimeout--;
                } else {
                    window.clearInterval(lTimer);
                    var lDiv = document.getElementById("throttle_div");
                    if (lDiv) {
                        lDiv.parentNode.removeChild(lDiv);
                        return true;
                    }
                }
            },
            1000 );
    }})();
Abdirizak Obsiye
  • 265
  • 5
  • 7
  • 29
  • Are you asking us to explain the code to you? Your question is a bit unclear. – Heretic Monkey Feb 27 '17 at 22:24
  • 2
    it adds a false sense of security while simultaneously pissing off end-users. – Kevin B Feb 27 '17 at 22:25
  • @MikeMcCaughan Yes, I'd like to understand how a 'invalid login: wait x amount of seconds' works? as in if the x part was a countdown. Also, the x value increases after so many tries until it is then blocked. – Abdirizak Obsiye Feb 27 '17 at 22:29
  • I'd suggest stepping through the code using your browser's developer tools. It is a vital skill to have and will help you better understand the code. [Here's an intro](http://stackoverflow.com/questions/988363/how-can-i-debug-my-javascript-code). – Heretic Monkey Feb 27 '17 at 22:38

1 Answers1

0

I will comment the code for you:

//when document is ready    
( document ).ready( function() {

        (function(){ //anonymous function
            // store in this variable the element
        var lTimeoutField = document.getElementById("throttle_sec"),
            //lTimeout = if variable lTimeoutField exist, add what contains, else put 0 , a?b:c is a quaternary operator, google search
            lTimeout      = lTimeoutField ? +lTimeoutField.innerHTML : 0;
         // if timeout is different from 0
        if (lTimeout) {
            // we set an interval, with the name 'lTimer', that will repeat at 1s, or 1000ms, window.setInterval has 2 parameters, a function and the time to repeat the functio basic this function every 1s check if lTimer is >0, and if is update the content in the field, substract-1, and continue until is <0
                var lTimer = window.setInterval (   
//function
            function() {
                   if (lTimeout > 0) {
                           // update the timeout field and substract that variable with -1
                            lTimeoutField.innerHTML = lTimeout;
                            lTimeout--;
                        } else {
                            // we clear the timeout
                            window.clearInterval(lTimer);
                            var lDiv = document.getElementById("throttle_div");
                            if (lDiv) {
                                lDiv.parentNode.removeChild(lDiv);
                                return true;
                            }
                        }
                    },
//time to repeat
                    1000 );
            }})();
Brada
  • 58
  • 5