Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6941509
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T12:53:42+00:00 2026-05-27T12:53:42+00:00

This function should write the time spent on the page to the div. It

  • 0

This function should write the time spent on the page to the div.
It was working fine with a window.onbeforeunload alert.. So why isnt this working right?

<script>
function tempo1() {
    var d1 = new Date();
    var t_day = d1.getDate();
    var t_hour = d1.getHours();
    var t_min = d1.getMinutes();
    var t_sec = d1.getSeconds();
    alert("Started at--> " + t_day + "days, " + t_hour + ":" + t_min + ":" + t_sec);

    function tempo2() {
        var d2 = new Date();
        var t_day2 = d2.getDate();
        var t_hour2 = d2.getHours();
        var t_min2 = d2.getMinutes();
        var t_sec2 = d2.getSeconds();
        var day = t_day2 - t_day;
        var hour = t_hour2 - t_hour;
        var min = t_min2 - t_min;
        var sec = t_sec2 - t_sec;
        document.getElementById('timespent').innerHTML = "Total--> '+day+'days, '+hour+':'+min+':'+sec";
    }
    setInterval(tempo2(), 500);
}
window.onload = tempo1();
</script>

<div id="timespent">??days, ??:??:??</div>

As a working axemple with a nice working algorithm:

<script>
startTime = new Date();
clockStart = startTime.getTime();
function initStopwatch() {
var NewTime = new Date();
return((NewTime.getTime() - clockStart)/1000);
}
function getSecs() {
var tSecs = Math.round(initStopwatch());
var iSecs = tSecs % 60;
var iMins = Math.round((tSecs-30)/60);
var sSecs ="" + ((iSecs > 9) ? iSecs : "0" + iSecs);
var sMins ="" + ((iMins > 9) ? iMins : "0" + iMins);
document.getElementById('timespent').innerHTML =  sMins+":"+sSecs;
window.setTimeout(getSecs,1000);
}
window.onload=getSecs;
</script>

<div id=timespent></div>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T12:53:43+00:00Added an answer on May 27, 2026 at 12:53 pm

    On the line

    window.onload = tempo1();
    

    …you’re calling the tempo1 function and assigning its return value to the load event on window. It’s exactly the same as x = foo();, where foo gets called and the return value stored in x.

    You don’t want those (), you just want:

    window.onload = tempo1;
    

    …which assigns the function reference, so it will get called when the load event occurs.

    Similarly, as Graham points out, you’re doing the same sort of thing later:

    setInterval(tempo2(), 500);
    

    which should be

    setInterval(tempo2, 500);
    

    There’s also a second error, on this line:

    document.getElementById('timespent').innerHTML = "Total--> '+day+'days, '+hour+':'+min+':'+sec";
    

    There, you’re quoting the string using double quotes ("), but you’re using single quotes when trying to substitute in the days, hour, min, and sec variables. You need to be consistent about which quote you use to start and end a string. Either is fine, but you have to pick one, so either all doubles:

    document.getElementById('timespent').innerHTML = "Total--> "+day+"days, "+hour+":"+min+":"+sec";
    

    or all singles:

    document.getElementById('timespent').innerHTML = 'Total--> '+day+'days, '+hour+':'+min+':'+sec';
    

    Note that you don’t need to wait for the load event, though. If you put your script at the bottom of the page (or anywhere below the “timespent” div — even immediately below it), you can just call tempo1 directly. The load event of the window object happens very late in the page load cycle, after all external resources (including all images) are loaded. If that’s what you want, great, but if not you have an alternative.

    References:

    • Google engineers on when you can access DOM elements
    • YUI Best Practices to Speed Up Your Website
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Recently I read a book( CleanCode ) in this book, FUNCTION SHOULD DO ONE
I came across the function InterlockedExchange and was wondering when I should use this
Consider the following snippet: $(document).bind('mousemove', function(e) { $('#someDiv').css({left: e.pageX+'px', top: e.pageY+'px'}); }); This should
I am trying to write a data calculated from this function in a file.
I'm having difficultly figuring out how to write this block of code... function myFunction(x,y,z)
This function appears to be a way to access all sorts of system values.
This function declaration gives me errors: ostream& operator<<(ostream& os, hand& obj); The errors are:
This function is written in ActionScirpt. What kind of decryption this is? Is there
This function of mine keeps on failing an autograder, I am trying to figure
Is this function declaration in C#: void foo(string mystring) the same as this one

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.