Click here to Skip to main content
15,895,799 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
HTML
<script type="text/javascript">
        //for timer...
        var mins
        var secs;
        function cd() {
            mins = 1 * m(document.getElementById("TxtTimer").value);
            secs = 0 + s(":01");
            redo();
        }

        function m(obj) {
            for (var i = 0; i < obj.length; i++) {
                if (obj.substring(i, i + 1) == ":")
                    break;
            }
            return (obj.substring(0, i));
        }

        function s(obj) {
            for (var i = 0; i < obj.length; i++) {
                if (obj.substring(i, i + 1) == ":")
                    break;
            }
            return (obj.substring(i + 1, obj.length));
        }

        function dis(mins, secs) {
            var disp;
            if (mins <= 9) {
                disp = " 0";
            } else {
                disp = " ";
            }
            disp += mins + ":";
            if (secs <= 9) {
                disp += "0" + secs;
            } else {
                disp += secs;
            }
            return (disp);
        }

        function redo() {
            secs--;
            if (secs == -1) {
                secs = 59;
                mins--;
            }
            document.getElementById("TxtTimer").value = dis(mins, secs);
            if ((mins == 0) && (secs == 0)) {
                window.alert("Time is up. Press OK to continue.");
                window.location.href = "MyScoreCards.aspx";
            } else {
                document.getElementById("form1").value = setTimeout("redo()", 1000);
            }
        }

        function init() {
            cd();
        }
        window. önload = init;
    </script>

    <style>
        /* for timer... */
        .timer {
            border: none;
            font-family: verdana;
            font-size: 16pt;
            font-weight: bold;
            border-right-color: #FFFFFF;
        }
    </style>
Posted
Updated 11-Apr-14 10:27am
v2
Comments
Maciej Los 11-Apr-14 16:27pm    
What's the issue?
Richard C Bishop 11-Apr-14 16:32pm    
Can you use UpdatePanels to only refresh portions that need it so your timer does not feel the effects of a full page post-back?
j snooze 11-Apr-14 17:47pm    
I'm thinking you don't want window.onload. Text box might be filled in yet. Especially if your javascript is above all the xhtml. Try

document.onreadystatechange=init;
function init()
{
if (document.readyState=="complete")
{
cd();
}
}

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900