Adsence750x90

Tuesday, January 25, 2011

Javascript Countdown timer

This post is mainly to understand, how to program a countdown timer using javascript. if you need to study more about javascript and javascript timer function go to w3schools.com and select Learn JavaScript.

Here I use two funtion to ActivateTimer() and Timer()

check the script

Check the Output


Example URL

Full code file

<HTML>
  <BODY>
  <div>
        <div ID="Label1" style="font-size:24pt;font-weight:bold;"></div>
        <input type="button" onclick="ActivateTimer();" value="Activate" />
        <script language="javascript" type="text/javascript">
            var min = 1;
            var sec = 59;
            var timer;
            var timeon = 0;
            function ActivateTimer() {
                if (!timeon) {
                    timeon = 1;
                    Timer();
                }
            }
            function Timer() {
                var _time = min + ":" + sec;
                document.getElementById("Label1").innerHTML =_time;
                if (_time != "0:0") {
                    if (sec == 0) {
                        min = min - 1;
                        sec = 59;
                    } else {
                        sec = sec - 1;
                    }
                    timer = setTimeout("Timer()", 1000);
                }
                else {
                    alert("Time is Over");
                }
            }
        </script>

    </div>
 </BODY>
</HTML>

Limiting file size or getting file size of Upload file in ASP.Net

this is a small code snippet to limit file size on file upload

Asp.Net FileUpload control's FileBytes.Length returns file size in Bytes.
Here we want file size in MB. so we want to divide size by 1024*1024.

ie
1024 kilobytes = 1 megabyte (MB)
1 Kilobyte = 1024 bytes

protected void Button1_Click(object sender, EventArgs e)
    {
        //checking size 
        double fileSize =(double) FileUpload1.FileBytes.Length;
        double fileinMB = fileSize / (1024 * 1024);
        if (fileinMB > limit)
        {
            Response.Write("Size is limited to 2MB");
        }
        else
        {
        //upload file
            Response.Write("Uploaded");
        }
       
    }