codeofnode
codeofnode

Reputation: 18629

How to set an timer in textbox using jquery..?

I have a text box on a jsp page. I want when the page loades the value of textbox should show how much time been the page loaded. Or in simple words i want to setup a countup timer in a textbox which should start immediately and automatically when the page loads. And when the user click on submit button the user navigates to another jsp page shows that how much seconds he took to click the submit button (button was on previous page).

I am doing following HTML

<input type="text" id="timeleft" name="timeleft" />

In jquery Script i am doing the following :

<script type="text/javascript" src="jquery-1.7.2.js"></script>
        <script type="text/javascript">
            $(document).ready(function(){
    var start = new Date;
                    setInterval(function() {
                        $('timeleft').val((new Date - start) / 1000);
                    }, 1000);     });
        </script>

But the timer is not visible in textbox...? What is the error..?

Upvotes: 0

Views: 2058

Answers (3)

Alex Ball
Alex Ball

Reputation: 4484

Try with this (Based on your code):

var start = new Date;
setInterval(timer, 1000);     

function timer() {
        $('#timeleft').val((new Date - start) / 1000);
    }

For int: $('#timeleft').val(parseInt((new Date - start) / 1000))

Upvotes: 1

Jithin
Jithin

Reputation: 2604

Just add the # for id selector.

Demo

Again if its not displaying in the html, please check there is only one element in your page with id="timeleft"

Upvotes: 0

laymanje
laymanje

Reputation: 833

You need to add the # to your selector.

$('#timeleft') - it is currently looking for a element named timeleft not the ID.

Also new Date will give you this - Wed Jul 18 2012 08:51:56 GMT-0400 (EDT)

if it is a timer you will want to get the time. try this for referance. http://www.w3schools.com/jsref/jsref_gettime.asp

Date Object: http://www.w3schools.com/jsref/jsref_obj_date.asp

Upvotes: 0

Related Questions