The duration spent on a website using AJAX technology

We conducted an online survey and need to accurately calculate the time spent by participants. After using JavaScript and PHP, we found that the calculated time is not completely accurate.

The original script was sending server requests every 5 seconds to update the time in the database.

Upon further research, I discovered that setTimeOut and setInterval are not reliable for accuracy. Can anyone suggest a better way to tackle this issue?

I tried replacing the Ping function with another method that calculates the difference between previous timestamp packets and now(); however, it also proved to be inaccurate.

If anyone has any other solutions or suggestions to address this problem, please advise.

Answer №1

Have you tried monitoring the 'onunload' event of the document to track when a user navigates away from your page? By sending a message at the beginning and end of their visit, you can calculate the total time spent on your site by measuring the interval between these messages.

Answer №2

In my opinion, the majority of setTimeout and setInterval implementations are programmed to pause for at minimum the duration you indicate. For precise measurements of browser uptime, consider utilizing JavaScript's date functionalities to compute it locally, and afterwards transmit the data to the server.

Answer №3

To ensure that requests are sent to the server every 5 seconds, include the current time (retrieved using new Date().getTime()) in each request from the client.

Record the timestamp of the first request in the database as the moment the user begins the survey.

For subsequent requests, calculate the total time elapsed by subtracting the initial request time from the arrival time. Once the user completes the survey by clicking a finish button, send a final request signifying survey completion.

Consider sending additional requests during document blur and focus events to monitor when users navigate away from and return to your page without closing it.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Personalized FullCalendar header title

Is there a way to display a unique header title for each calendar in my collection of 16? I've been trying various modifications to the code snippet below with no success: firstDay: <?php echo $iFirstDay; ?>, header: { left: 'prev,next ...

substituting the deep watcher in Angular

In my application, I am working with a data object called fulldata, which consists of an array of objects. fulldata = [ {'key': 'abc', values: {.....},....}, {'key': 'efg', values: ...

In Laravel, I am facing an issue where I am unable to trigger any event on the data that I have appended using

Currently using Laravel 5.6 Implemented the following event to append div.categoryAjaxId: The code works as expected and successfully appends my content. //--------- get product ------------------------- jQuery('.orderProduct').click(function( ...

Tips on incorporating a firebase object into your Angular application using $scope

As a beginner in coding, I would greatly appreciate a simple answer. I am attempting to treat a Firebase object as a JavaScript array for use in HTML via $scope. What is the most effective approach? Database: database Current code: var mainApp = angula ...

The efficiency of XSL Template is significantly impacting loading time

Hello there, I am facing a challenge with my webpage's loading speed due to the code provided below. Can you assist me in optimizing it? <xsl:template match="Category" mode="CategorySelectorScript"> <xsl:variable name="ThisCateg ...

Ensuring Stringency in JQuery's '$' Selector

I have a specific data attribute within the div element that is displayed in the HTML. <div my-custom-attrib="1".../> <div my-custom-sttrib="2"...> Now, in JQuery, I am attempting to filter between the div elements based on the value of the c ...

Padding for the initial element in a carousel display

I am currently working on implementing owl carousel with stage padding. My goal is to use the carousel with loop:false, and have the first item displayed without left padding, while maintaining consistent padding for both items on the left and right after ...

Updating the iFrame source using jQuery depending on the selection from a dropdown menu

I want to create a dynamic photosphere display within a div, where the source is determined by a selection from a drop-down menu. The select menu will provide options for different rooms that the user can view, and the div will contain an iframe to showca ...

What is the process for setting up basic http authorization using angular.js?

My backend setup follows a structure similar to what is explained in this article. I am looking to make a GET request using angular.js, just like curl does it, with authorization via HTTP headers: $ curl -u miguel:python -i -X GET http://127.0.0.1:5000/a ...

What's the CSS equivalent of Java's window.pack() method?

I'm relatively new to css and I'm attempting to create a border around a <div>. My goal is for the border to be limited to the size of the elements inside the div and adjust dynamically in proportion to any new objects that may appear or di ...

Tips on transforming a JSON array object into a JSON array

**Background:** I have been utilizing lodash to eliminate the empty key from my JSON data. However, upon removal of the keys, it transforms my array into an object. For instance: { "projection": "Miller", "series": [ { "mapPolygons": { ...

Accessing a variable from another HTML using JavaScript

I'm currently attempting to access a variable from another HTML file using JS. Specifically, I have a file (file1.htm) that opens a dialog box, and I want to send the information of the selected file to another file (file2.htm) in order to modify a v ...

Select a checkbox automatically after receiving an ajax response

I am currently working with a form that contains multiple checkboxes like the one below: <label class="checkbox"> <input type="checkbox" id="user_ids_for_edit" name="user_ids_for_edit[]" data-toggle="checkbox" data-toggle="checkbox" value="14"&g ...

Struggling to retrieve the tagName attribute when clicking in JavaScript

I am trying to extract the tagName of any element within an iframe when it is clicked. Unfortunately, my JavaScript code is not working as expected. As a beginner in JavaScript, I would appreciate some guidance on where I might be going wrong. <div cl ...

Is there a way to use JavaScript to add content to a document and then facilitate the download of that document?

Is there a way in JavaScript to write content to a JSON or TXT file and then download it, similar to how it is done in Python? const content = "Hello"; const link = document.createElement('a'); link.setAttribute('download', 'FileTo ...

Suggestions for managing AngularJS within ASP.NET Web Forms?

Recently, I integrated AngularJs into a website that is built with asp.net webforms. I discovered that when using ng-Submit on a button, the form also triggers a Post call. How can I prevent the form from automatically submitting so that Angular can perf ...

The onblur event is triggering prior to the value being updated

There are two input fields within a <form> element. I am trying to retrieve the value of one input field (dpFin) once it has been changed. The issue is that when I attempt to get the new value inside the event using var endDt = document.getElementByI ...

Modify the onerror function of the image tag within the onerror function

Here is a way to display images using the img tag: If 1.jpg exists, show 1.jpg. If not, check for 2.jpg and display it if it exists. If neither 1.jpg nor 2.jpg exist, display 3.jpg. <img src="1.jpg" onerror="this.src='2.jpg'; this.oner ...

Populate an HTML select with data as users click the create button

Encountering an issue when loading data into an HTML select after users press or click a button. The situation is as follows: A button is available to create another button dynamically Upon clicking the created button, a form is displayed Once the form i ...

The field "addWorkout" cannot be queried on the type "Mutation"

My journey with GraphQL has just begun, and after resolving a reference error in my previous question, I have encountered a new challenge. It appears that adding a workout is not working as expected, as the schema does not recognize it as a mutation field. ...