Guide on how to execute a JavaScript function every 15 seconds for a duration of 5 minutes only, along with instructions on monitoring page idleness over a specific period

Recently, I have created a function that automatically calls every 15 seconds.

setInterval(
  function () {
    check_alive_status();
  }, 
  15000 // every 15 seconds
);

However, I now have a new requirement where I only want this function to run for the first 5 minutes and then stop. Is there a way to achieve this?

In addition to this, I also need to check if there are any background processes such as ajax calls or file uploads happening on the webpage. If there are no active processes and the page is idle, I want to log out the user after 15 minutes. How can I implement this too? Thank you!

Answer №1

// execute a task every 15 seconds

var intervalId = setInterval(function(){
 // perform the task here 
}, 15000);

// stop the task after 5 minutes

setTimeout(stopTask, 5 * 60 * 1000)

// clear the interval

function stopTask() {
    clearInterval(intervalId);
}

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

What is the method for inserting form control values into a QueryString within HTML code?

Struggling with passing HTML form control values into a QueryString for page redirection. While I can easily input static values into a QueryString and retrieve them using PHP's GET method, I am encountering difficulties when it comes to dynamic valu ...

Using JQuery to switch classes and activate events

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <body> <span class="fake_h">Too</span> </body> <script> $('.fake_h').click(function() { $(this).addClass( ...

Permit the use of HTML tags within an Angular controller or view

My controller : LandingApp.controller('LandingCtrl2', function($scope){ $scope.articles = [ { 'imageSrc' : IMG_DIR + 'spec9.jpg', 'title' : 'Stencils', ...

Can dynamic CSS imports be unloaded in a React application?

I am facing an issue with loading two files using react.lazy and suspense: import React, { Suspense, lazy } from "react"; import { Route, Redirect } from 'react-router-dom'; const MainLayout = lazy(() => import('Components/Layout/MainL ...

Tips for assigning information from a react hook within a function or event

Currently, I am in the process of learning how to create hooks in order to efficiently reuse data that needs to be modified across different components. For my project, I am utilizing Material UI's Tabs and require the use of a custom hook called use ...

One way to create a unique JavaScript Module Pattern that retrieves the default value of a

I'm struggling to retrieve the latest private property from a Module, as I keep getting the initial value instead of the most recent one. Upon submission of the form and triggering onSuccess, I assign partnerId = 10. Subsequently, upon clicking an e ...

Tips for updating a React state while using the history API

In an effort to simplify and stay focused on the problem, I recently wrote a brief piece of code. Within this code, there is a component containing a Dialog that plays a role in a commercial process. This Dialog allows users to input information such as no ...

Auth0 and Vue.js work together seamlessly to easily redirect users back to their previous page

I have integrated auth0 into my Vue.js application for handling logins. Within my AuthService, I manage all the auth0 operations. In the constructor, I instantiate a new auth0 object and set the default redirectUrl to the desired location: import auth0 f ...

Unable to access a hyperlink, the URL simply disregards any parameters

When I click an a tag in React, it doesn't take me to the specified href. Instead, it removes all parameters in the URL after the "?". For example, if I'm on http://localhost:6006/iframe.html?selectedKind=Survey&selectedStory=...etc, clicking ...

Ways to identify when a user presses the back or forward arrow on a browser using JavaScript or jQuery for a non-single page application (

There are only 2 routes available: / and /about When on the following URLs: localhost:8080 -> it displays the home page localhost:8080/about -> it shows the about page If a user opens the home page, clicks on the about button, and goes to the abou ...

Automate the process of saving information to Google Sheets using Google AppScript

I have a Sheet named 'Automatic' where I've imported a set of data using IMPORTXML. My goal is to update this data list daily at the same time to create a database with various stock quotes over time. Is there a way to accomplish this usin ...

The AJAX email submission form is not functioning properly

Recently, I have upgraded my email sign-up form on the website to include validation. However, after adding the validation, the form no longer sends and an error message is not displayed. Upon inspecting, I found the following error: TypeError: null is ...

No action detected following the import into Redux form

This is my very first project in React. After completing a training course on Udemy, I have reached the following code but am stuck trying to integrate the action into the container (Is Container correct?). Here is the code I have put together from variou ...

Postman seems to be functioning correctly while AXIOS is encountering issues

I've encountered a strange issue with my MERN app. When I use Postman to send a PUT request to my API, it successfully updates both the API and MongoDB. However, when performing the same action on the front-end, the API does not update even though the ...

What is the reason for the index.html file not connecting to the local .css files and .js file?

I'm currently using node.js to send an .html file that includes the following tags: <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <meta name="description" content="This is my personal profile website" /> <link r ...

Is it universally compatible to incorporate custom attributes through jquery metadata plugin in all web browsers?

Picture this: a fictional markup that showcases a collection of books with unique attributes, utilizing the metadata plugin from here. <div> Haruki Murakami </div> <div> <ul> <li><span id="book5" data="{year: 2011} ...

Unable to assign focus to textbox

In my chrome extension, I have implemented two text boxes - one for entering a URL and the other for LDAP. Upon clicking on the extension icon, a popup page opens where I automatically fill the URL text box with the current page's URL using the code s ...

Implement authentication verification on all child endpoints within an express router

I have an express router set up and I want only authorized users to access its routes. I am currently using passport middleware. Instead of adding a check for req.user in every endpoint, is there a more efficient way to handle this? router.get("/", asyn ...

Issue encountered while attempting to write to csv-file using npm csv-writer - no error message displayed and function not working as

In my electron renderer.js process, I am attempting to write to a CSV file. Unfortunately, most of the time, the writing process doesn't work as expected, and the addition of .then is not executed. Despite this, no error messages or indications of mis ...

Leverage the Google Maps JavaScript API v3 to retrieve details for multiple locations using the getDetails(request, callback

I successfully integrated the Google Maps JavaScript API v3 to create a personalized store locator for our company's website. While the current code is functional for two stores, it lacks scalability due to the unconventional methods used. My impleme ...