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

  • Home
  • SEARCH
  • 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 7572779
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T16:00:44+00:00 2026-05-30T16:00:44+00:00

I am trying to write a simple JavaScript page timer for a web app.

  • 0

I am trying to write a simple JavaScript page timer for a web app. The timer will record the time at the press of a button (as the user enters the page) and record the time again at the press of a button (when the user leaves the page) The difference of the 2 times will give the time the user spent on the page. If they stayed on the page for more than 2 seconds the number of seconds will be recorded to the local database on the hand held.

The problem I am having is that the following code will not work if the alert in the starttime function is commented out.

$(document).ready(function() {
    $('div.m1-root').click(function () {
      //simulate the pagetransition events in mobile framework
        pagename= $(this).attr('id'); 
        starttime();
    //alert("You are leaving\n" + pagename + "\n you were here for \n" + time.toFixed(0) + " seconds"); // This alert will fire but only record the proper time if the next alert is fired
    });
}); 

function starttime()
{
//create a starting timestamp, number of milliseconds since midnight Jan 1, 1970
start = new Date().getTime();
//alert(+ start); // if this alert is commented out the time is not started
    pagetime();
}

function pagetime(pagename){
time = (new Date().getTime() - start) / 1000;
//alert("you have been on " + pagename + " for \n" + time.toFixed(0) + " seconds");
//before we transition, log previous page and time
//if time is greater than 3 seconds we log it (I changed it to -1 so that I would see any figure while testing)
if (time >-1)
{
//DataBase update
db.transaction(function(tx) {tx.executeSql('INSERT INTO CBNapp_Usage VALUES (time)',function(tx,result) {},function(tx,Error) {});});   
//alert("You spent " + time.toFixed(0) + " seconds on " + pagename);
}
}

I have looked on SO and the web and have come across similar situations but I can not seem to get any of those ideas to work here. I understand that this may be a timing problem and have tried a setTimeout or return true to delay things but it does not work.

Could someone please take a look at the code and suggest how I might be able to get it to work properly?

This is my first from scratch JS. Please provide specific examples so I can follow them. The database is also not recording but I will work on that later. If you have any other specific suggestions as to how to improve this I would welcome them.

Thanks for taking the time to help.

Ted

  • 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-30T16:00:46+00:00Added an answer on May 30, 2026 at 4:00 pm

    It looks like you’ve got all your code inside the button click handler. It only appears to be working when you have the alert, because the alert creates the appearance of delay.

    You need to put starttime() outside of the click handler, and it shouldn’t be calling pagetime().

    $(document).ready(function () {
        starttime(); // this will run as soon as the page loads
        $('div.m1-root').click(function () {
            pagetime();
            // leave the page
        });
    });
    
    function starttime() {
        start = new Date().getTime(); // start is global
    }
    
    function pagetime() {
        var time = (new Date().getTime() - start) / 1000;
        // do something with time, like logging it to your db
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I’m a newbie to web programming and trying to write a simple app, which
I'm trying to write a simple javascript function that will clone some html input
I am trying to write a simple tool using Shoes. This will indent code
I'm trying to write a simple rule such that: /page gets implicitly routed to
I am trying to write a simple if statement in javascript (jquery library) that
I'm trying to write a simple app that has several internal pages. From #page2
I'm trying to write a simple Javascript(jQuery) function that randomly displays 6 Divs out
I'm trying to write some simple chat client in javascript, but I need a
I'm trying to write a simple python web application using the Tornado web server
I am trying to write a simple decrypt function in JavaScript that would take

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.