Click here to Skip to main content
15,886,110 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
HI,

I am new in Asp.net MVC, and currently am doing a project in MVC 3 . And i need to show the Elapsed Time when a div is load. I need the client side Elapsed time in multiple <div> tags.

And i got a Time Elapsed javascript below mentioned. but i cant use this code to show in multiple <div> tags.

For eg : I have four <div> tags. and i have to show the Time Elapsed in these four <div> tags..



XML
<script>
var seconds = null;
var ticker = null;

function startTimer( )
{
    seconds = -1;
    ticker = setInterval("tick( )", 1000);
    tick( );
}

function tick( )
{
    ++seconds;
    var secs = seconds;
    var hrs = Math.floor( secs / 3600 );
    secs %= 3600;
    var mns = Math.floor( secs / 60 );
    secs %= 60;
    var pretty = ( hrs < 10 ? "0" : "" ) + hrs
               + ":" + ( mns < 10 ? "0" : "" ) + mns
               + ":" + ( secs < 10 ? "0" : "" ) + secs;
    document.getElementById("ELAPSED").innerHTML = pretty;
}
</script>
<body onLoad="startTimer( )">
...
The elapsed time is now <span id="ELAPSED"></span>
...
</body>



Can you please help me.......


Thanks and Regards,

Dileep
Posted
Updated 2-Aug-12 4:32am
v3

I would suggest to follow below steps:

1) call startTimer( ) in each of the page where you want to show the elapsed timer.

2) In the Tick method pass the span id where you want to show the elapsed time.
So each time you call tick it will show the elapsed time in the given span id.
That span can be in any page and inside any DIV.

Below is the code for tick method.

JavaScript
function tick(spanId )
{
    ++seconds;
    var secs = seconds;
    var hrs = Math.floor( secs / 3600 );
    secs %= 3600;
    var mns = Math.floor( secs / 60 );
    secs %= 60;
    var pretty = ( hrs < 10 ? "0" : "" ) + hrs
               + ":" + ( mns < 10 ? "0" : "" ) + mns
               + ":" + ( secs < 10 ? "0" : "" ) + secs;
    document.getElementById(spanId).innerHTML = pretty;
}



Hope it helped.
 
Share this answer
 
Comments
dilzz 2-Aug-12 7:49am    
Hi,

When i used as u mentioned above. The timer will appear, but it didn't works. Here is the below code that i tried it. please advice me.

<pre lang="xml"><head>

<script>
var seconds = null;
var ticker = null;

function startTimer(spanid) {

seconds = -1;
ticker = setInterval("tick(spanid)", 1000);
tick(spanid);
}

function tick(spanid) {
++seconds;
var secs = seconds;
var hrs = Math.floor(secs / 3600);
secs %= 3600;
var mns = Math.floor(secs / 60);
secs %= 60;
var pretty = (hrs < 10 ? "0" : "") + hrs
+ ":" + (mns < 10 ? "0" : "") + mns
+ ":" + (secs < 10 ? "0" : "") + secs;
document.getElementById(spanid).innerHTML = pretty;
}


</script>
</head>
<body>
<input type="submit" onClick="startTimer('ELAPSED')" /><span id="ELAPSED"></span>
<br />
<input type="submit" onClick="startTimer('ELAPSED1')" /><span id="ELAPSED1"></span>
</body></pre>
<pre lang="HTML"></pre>

Thanks,

Dileep
I gave you just a indication but you could not follow me.
If you want it to run with all of your independent timers then you have to play with javascript closure. Please see the below sample its tested and running.

Have a look and let me know if you still not it up and running.

HTML
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Timer</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <input type="button" onclick="startTimer('timer1');" value="starttimer1" />
        <span id="timer1"></span>
        <br />
        <input type="button" onclick="startTimer('timer2');" value="starttimer2" />
        <span id="timer2"></span>
    </div>
    </form>
</body>
<script type="text/javascript">
    var startTimer = function (idtimer) {
        var objtimer = document.getElementById(idtimer),
            refreshTimer = showtimer(objtimer);
        setInterval(refreshTimer, 1000);
    }, showtimer = function (objElment) {
        var element = objElment, seconds = -1;
        return function () {
            ++seconds;
            var secs = seconds;
            var hrs = Math.floor(secs / 3600);
            secs %= 3600;
            var mns = Math.floor(secs / 60);
            secs %= 60;
            var pretty = (hrs < 10 ? "0" : "") + hrs + ":" + (mns < 10 ? "0" : "") + mns + ":" + (secs < 10 ? "0" : "") + secs;
            if (element) {
                element.innerHTML = pretty;
            }
        };
    };
</script>
</html>


You can also view the running sample at Fiddle
 
Share this answer
 
v2
Comments
dilzz 16-Aug-12 2:50am    
Thank you Sir, Thanks a lot........

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