The JQUERY code for refreshing a div requires a timeout delay

I'm looking for a way to refresh a specific div on my website that's used for chat. Here's the code I currently have:

var refreshId = setInterval(function() {
    $('#chat_grab').load('chat_grab.php?randval=' + Math.random());
}, 5000);

The issue I'm facing is that if someone leaves the page open, this code will keep looping indefinitely. Is there a solution to make it timeout after, let's say, 10 minutes of the base page not being refreshed?

Answer №1

let startTime = new Date();
let intervalId = setInterval(function() {
    $('#chat_grab').load('chat_grab.php?randval=' + Math.random());
    if((new Date() - startTime) > (10 * 60 * 1000)) {
        clearInterval(intervalId);
    }
}, 5000)

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

How can I expand and collapse elements using Angular?

I'm looking to implement a collapsible feature. When the user clicks on the "Section Title", I want the corresponding information to collapse or expand. @Component({ selector: 'knowledge-base', template: ` <div *ngFor="let sect ...

Rails backend is struggling to receive Crossrider ajax post requests with JSON payload

I am encountering an issue where my attempts to post a JSON object to a remote server (Rails) are failing. The POST parameters seem to be converted to a url-encoded string instead of being sent as 'application/json'. Here is an example of what I ...

Trouble with Fetch in JS and PHP not sending parameters

Having trouble getting a PHP function to accept data from JavaScript, despite the PHP class working elsewhere in the application. The getTopFive() function works fine, but data insertion isn't happening as expected. Below is the code snippet along wit ...

Create essential CSS for every .html page using Gulp

Recently, I came across a useful npm package recommended by Google for generating critical CSS: var critical = require('critical'); gulp.task('critical', function (cb) { critical.generate({ base: 'app/', ...

Can the ng-keypress listen for arrow key presses?

I'm looking to implement a functionality similar to the konami code "up, up, down, down, a, b, a, b, enter" -> triggering an action. Is it feasible to detect arrow key presses using ng-keypress in AngularJS? It doesn't seem to be working as e ...

Is there a way to automatically add a column to one table while adding a row to a different table at the same time (with the column name being the row's id

Is there a way to write MySQL code that automatically adds a corresponding column in another table whenever a row with a primary key ID is added? The name of the column should match the ID of the newly added row. I attempted the following approach, but it ...

Angular 5 with Typescript encountered a failure in webpack due to the absence of the property "data" on the Response

I am encountering an issue during webpack compilation. It compiles successfully if I remove .data, but then the page crashes with calls from template->component (which in turn calls a service). Here is the error I am facing: ERROR in src/app/compone ...

Unable to retrieve the path during an AJAX request in PHP and JavaScript

I'm struggling with passing data to the server-side using an ajax call. It's giving me an error saying 'The required path not found'. I am working with CodeIgniter for the MVC framework. Below is a snippet of the code: var url = "http: ...

Utilize Jquery to dynamically update form input in real time based on checkbox selections

I am working on a form that requires real-time calculation of GST (Goods and Services Tax) directly within the form (GST = Price/11) This functionality has been implemented with the following script. Additionally, the calculation needs to be adjust ...

What are some alternatives to the APC user data cache for PHP 5.5?

In the latest version of PHP 5.5, zend opcache is now included by default, rendering APC almost obsolete for most users. However, when it comes to caching user data like with apc_store & apc_fetch from APC, what alternatives are available? One practi ...

The perplexing behavior of RxJS Observables with Mongo Cursors

Recently, I've been working on converting a mongo cursor into an observable using my own RxJS implementation. Despite finding numerous solutions online, I wanted to challenge myself by creating one from scratch. I would greatly appreciate it if someo ...

Replace text using the str_replace function

I have a total of seven lines of text that I need to manipulate using PHP. My first task is to filter out any lines that do not contain the word 'MARCO1998'. Next, from the remaining lines, I need to extract only the 1-3 numbers that come after ...

"Implementing a loading spinner on the submit button to ensure single submission and prevent

In my code, the master page includes frames for navigation purposes. On clicking the submit button on any screen, I want to disable the page to prevent further user interference. To achieve this, a div is used in the master page and the following code is a ...

Pass information from one .jsp file to a JavaScript function in another .jsp file

Here is the content of OneEmployee.jsp: <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> <form> <td> <c:out value="${emp.getEmpid()}"/></td> <td> <input type="text" id="fname" value="<c:out v ...

Assign individual heights to multiple iFrames according to their content's height

Dealing with multiple iframes on a single page. Each iframe is sourced from my domain. The goal is to automatically calculate and set the height of each iframe on the page. The current code sets all iframe heights to match that of a specific iframe: fun ...

Dealing with incoming data in a yii2 RESTful service

When sending an array using $http post, I follow this approach: var results = services.transaction('orderdetails/insert', {customer_id: $scope.order.customer_id, data: $scope.orderDetail}); The variable $scope.orderdetail ...

What purpose does the next() function serve in Express.js?

Using this script to kickstart my next js app. I can't share the entire script, but I do need some assistance with the following: What is the purpose of compression? What is the importance of using helmet? What does next({dev}) do? const express = re ...

Check to see if there is a value present in a JavaScript array

I am trying to validate the content of the data array in the code below. My goal is to ensure that when a user enters a packageid (a variable in the code), and if that packageid does not exist, the "else" statement in the conditional should be triggered. ...

Apple Safari 14.0.3 restricts JavaScript from setting a cookie expiry date beyond one week from the current date

This is my second time reaching out with this question, but after plenty of trial and error experimentation, I have gathered much more information to share. I have been attempting different methods to set a cookie in Safari using JavaScript 'document ...

Provide input data to the function for delivery purposes

Creating a simple webpage with a form input that triggers a script to request JSON data from the server based on the user's input. The challenge is passing the value from the input field to a function for processing. var search = document.querySele ...