vendredi 30 décembre 2016

How to make a very simple countdown timer in Javascript?

I'm making a simple game which generates random numbers and user has to enter a value, if users value matches the random value generated, the user wins basically this is a beginner project. I want to make a small counter so that user has a limited time to put in the value and I want that limited time to show on the screen in a label. Lets say that you have to put the value under 30 secs and the timer will count from 1 to 30 every second. The counting from 1 to 30 will update in the label every second. This is the logic I'm trying to work on right now and I can't figure out any other way... If I've done some mistake in code please comment or if you have much more simpler way please post it down below. (pls dont vote down im at threat of account suspension) Heres the part of my timer code:

if(timer <= 30)
    {
        for(var i = 0;i >= 30;i++)
        {
            setInterval(null,1000);
            timer++;
            document.getElementById("counter").innerHTML = timer+" seconds wasted";
        }
        alert("Time is over, you lost by "+s);
    }

Aucun commentaire:

Enregistrer un commentaire