Jump to content

HTML how to create a timer

Go to solution Solved by mikat,

ty how would i put

like

 

Youve been on this page (time) seconds

<script>time=0;window.setInterval(function(){time+=1; document.getElementById('myfancytext').text = "You've been on this page for " + time + " seconds"}, 1000);</script><p id="myfancytext"></p>

im currently working on a web browser game and i need a way for it to time how long you've been on the page how would i go about doing this

My Most Succesful Posts: Favourite Part of Linus's Channel http://linustechtips.com/main/topic/500392-your-favourite-part-of-linuss-channels/ Like It? Leave A Like!


Should Linus Make Mobile Phone Videos http://linustechtips.com/main/topic/500416-should-linus-make-mobile-videos/#entry6682477 Like It? Leave A Like!


Favourite LMG Member http://linustechtips.com/main/topic/500420-favourite-lmg-member/ Like It? Leave A Like!

Link to comment
https://linustechtips.com/topic/500349-html-how-to-create-a-timer/
Share on other sites

Link to post
Share on other sites

im currently working on a web browser game and i need a way for it to time how long you've been on the page how would i go about doing this

<script>time=0;window.setInterval(function(){time+=1}, 1000);</script>
the time variable is the amount of seconds since the script started running :)

you could do it with new Date() and that sort of stuff, but that's way more work and more precise but you wouldn't need that level of preciseness :)

Link to post
Share on other sites

ty how would i put

like

 

Youve been on this page (time) seconds

My Most Succesful Posts: Favourite Part of Linus's Channel http://linustechtips.com/main/topic/500392-your-favourite-part-of-linuss-channels/ Like It? Leave A Like!


Should Linus Make Mobile Phone Videos http://linustechtips.com/main/topic/500416-should-linus-make-mobile-videos/#entry6682477 Like It? Leave A Like!


Favourite LMG Member http://linustechtips.com/main/topic/500420-favourite-lmg-member/ Like It? Leave A Like!

Link to post
Share on other sites

ty how would i put

like

 

Youve been on this page (time) seconds

<script>time=0;window.setInterval(function(){time+=1; document.getElementById('myfancytext').text = "You've been on this page for " + time + " seconds"}, 1000);</script><p id="myfancytext"></p>
Link to post
Share on other sites

You can use javascript:

            timer = 0;            function countTime() {                timerTxt = document.getElementById("timerTxt"); // you give that ID to an element in your html                start = setInterval(function () {                    timerTxt.innerHTML = "You have been on this page for " + timer + " seconds";                    timer++;                }, 1000); // will increment timer every 1 second (1000ms) and change the text of timerTxt to the timer value             }

Benefit of this is you can use the timer variable since it's a global variable.

 

Then you add

onload = "countTime();"

in your <body ... > tag

Link to post
Share on other sites

ty for the help

My Most Succesful Posts: Favourite Part of Linus's Channel http://linustechtips.com/main/topic/500392-your-favourite-part-of-linuss-channels/ Like It? Leave A Like!


Should Linus Make Mobile Phone Videos http://linustechtips.com/main/topic/500416-should-linus-make-mobile-videos/#entry6682477 Like It? Leave A Like!


Favourite LMG Member http://linustechtips.com/main/topic/500420-favourite-lmg-member/ Like It? Leave A Like!

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×