Informing the parent window of the child window's activities in order to adjust the timer for the user timeout feature

Within my Jquery function, I have implemented a feature that darkens the screen after a period of user inactivity. This triggers a pop-up window giving the user the option to stay logged in by clicking a button. If there is no response within the set time frame, the application window will be closed and the user logged out.

This functionality is coded in ASP.NET (VB). Although we do not utilize master pages, our header, footer, and navigation elements are contained in a parent page where my Jquery script is invoked from via an IFrame.

The issue arises when working within a child window - the parent window does not detect any activity and proceeds with the automated process at the scheduled time.

Despite several attempts, including ensuring the event handler functions properly and calls the parent window function, the timer fails to reset.

Here's the code snippet from the parent window:

<script language="javascript" type="text/javascript">  
function window.reportChildActivity() { 
    SESSION_ALIVE = true; 
    window.setTimeout("pop_init()", SESSION_TIME); 
} 
</script>

And this is how it is handled in the child window:

<script type="text/javascript">  
$(document).bind("mousedown keydown blur", function() { 
    window.parent.reportChildActivity(); 
}); 
</script>

No matter how much interaction occurs in the child window, the Jquery timeout function gets triggered upon reaching SESSION_TIME for the first instance. Consequently, multiple redundant pop-up windows prompt the user to click for continued access. It appears as if the events are queued up and generate these duplicate windows when executed. Can anyone identify what might be causing this issue? Appreciate any insights!

---- EDIT ----- Provided below are additional functions used alongside my pop_init function:

// remove all added objects and restart timer
function popup_remove() {
    $("#popup_window").fadeOut("fast", function() { $('#popup_window,#popup_overlay').trigger("unload").unbind().remove(); });
    //if (typeof document.body.style.maxHeight == "undefined") {//if IE 6
    $("body", "html").css({ height: "auto", width: "auto" });
    $("html").css("overflow", "");
    //}
    window.setTimeout(pop_init, SESSION_TIME);
}

// session ajax call from button click
function session_refresh() {
    SESSION_ALIVE = true;
    $(".buttons").hide();
    $("#popup_message").html("<center><br />Thank you!  You may now resume using the application.<br /></center>");
    window.setTimeout(popup_remove, 1000);
    $("#popup_window").fadeOut("slow", function() { $('#popup_window,#popup_overlay').trigger("unload").unbind().remove(); });
    window.setTimeout(pop_init, SESSION_TIME);
}

function popup_expired() {
    if (!SESSION_ALIVE)
        window.close();
}

// Main popup window handler
function pop_init() {
    // show modal div
    $("html").css("overflow", "hidden");
    $("body").append("<div id='popup_overlay'></div><div id='popup_window'></div>");
    //$("#popup_overlay").click(popup_remove);  // removed to make sure user clicks button to continue session.
    $("#popup_overlay").addClass("popup_overlayBG");
    $("#popup_overlay").fadeIn("slow");

    // build warning box
    $("#popup_window").append("<h1>Warning</h1>");
    $("#popup_window").append("<p id='popup_message'>Your session is about to expire.  Please click the button below to continue working without losing your session.</p>");
    $("#popup_window").append("<div class='buttons'><center><button id='continue' class='positive' type='submit'><img src='images/green-checkmark.png' alt=''/> Continue Working</button></center></div>");

    // attach action to button
    $("#continue").click(session_refresh);

    // display warning window
    popup_position(400, 300);
    $("#popup_window").css({ display: "block" }); //for safari using css instead of show
    $("#continue").focus();
    $("#continue").blur();

    // set pop-up timeout
    SESSION_ALIVE = false;
    window.setTimeout(popup_expired, 30000);
}

Answer №1

One approach is to store the setTimeout function in a global variable and clear it before setting a new one. Here's an example:

var timer=false;
window.checkSession = function() {
    if(timer!==false) {
        clearTimeout(timer);
    }
    SESSION_ACTIVE = true; 
    timer=window.setTimeout(updateSession, SESSION_DURATION); 
}

Check out this demo: http://jsfiddle.net/qA7jC/2/

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 could be causing ng-submit to not successfully transmit data?

I'm currently going through this Yeoman tutorial, but I'm encountering some issues. The new todo is not being added to the $scope.todos as expected, and I'm struggling to identify the reason behind it. You can access the code here: Upon c ...

Transferring files to Django using AJAX

I am struggling with the process of uploading files to django using ajax. The upload is done within a modal window. The Form <div class="modal fade bs-example-modal-lg" id="fileUploadModal" role="dialog" aria-hidden="true"> <div class="modal ...

Having difficulty implementing two-way binding on SELECT element within Angular JS programmatically

Struggling with implementing two-way binding for SELECT elements. Attempting to dynamically change the selected element through code. While I've come across examples on Stackoverflow for binding the change event for SELECT, finding resources on the ap ...

Show a modal component from another component in Angular 2

As a newcomer to Angular, I'm working on a modal component that changes from hiding to showing when a button with (click) is clicked. The goal is to integrate this modal into my main component, allowing me to display the modal on top of the main conte ...

Tips for preventing the use of nested functions while working with AJAX?

Consecutively making asynchronous calls can be messy. Is there a cleaner alternative? The issue with the current approach is its lack of clarity: ajaxOne(function() { // do something ajaxTwo(function() { // do something ajaxThree() }); }); ...

Steps to retrieve a rejected object with an error stored in it in the following .then() function

Within my chain of promises, there is a specific promise that needs to log an error but pass the remaining data to the next .then() const parseQuery = (movies) => { return new Promise((resolve, reject) => { const queries = Object.keys( ...

Transferring token values between collections in POSTMAN - AUTOMATION | NEWMAN: A step-by-step guide

My goal is to streamline my unit test cases by utilizing the POSTMAN Collections API & NEWMAN. I successfully created two test cases that are performing as expected. Upon exporting the collection from POSTMAN, I proceed to generate the test report using NE ...

Transferring information from socket.io to vue.js

I am currently facing an issue with passing socket.io data to a Vue.js element. Despite going through the Vue documentation multiple times, I have not been able to find a solution. The data is being sent to the client via socket.io and successfully logged ...

Issue with passing parameter values in MVC Html.ActionLink

Currently, I am experimenting with MVC for a demonstration and have managed to put together some components. However, I am encountering difficulties with an Html.ActionLink. The goal is to display a series of dropdown lists to the user that must be selecte ...

Implementing a NextJS client component within a webpage

I am currently working with NextJS version 14 and I am in the process of creating a landing page. In one of the sections, I need to utilize the useState hook. I have specified my component as "use-client" but I am still encountering an error stating that " ...

The callback in Jquery getJSON does not execute even when a valid JSON response is received

My server is sending valid JSON objects (as verified by jsonlint.com) that have this structure: "{\"encryption\": {\"key\": \"gKV0oPJwC5CBQxmn\"}}" This is how my HTML file looks: <html> <head> <title&g ...

Tips for increasing the height of a Kendo-UI grid to 100% within an AngularJS 1.x environment

Encountering an issue, I needed a Kendo UI Grid widget with its height set to 100% of the surrounding <div>. Attempting basic CSS styling on the grid proved unsuccessful: html, body { height:100%; } .expand-vertical { height: 100%; min-h ...

Encountering a 405 Error with C# Web API when attempting a GET

As someone who has recently transitioned from desktop development to working with restful APIs, I am facing an issue with a 405 error when attempting a GET request on a controller. The code for my controller is as follows: public class ApplicantsControll ...

Error: Attempted to access the 'state' property of an undefined object

I am working with the following function: extractCountries: function() { var newLocales = []; _.forEach(this.props.countries, function(locale) { var monthTemp = Utils.thisMonthTemp(parseFloat(locale["name"]["temperature"])); if(p ...

Convert a String to JSON using either JavaScript or jQuery

Currently, I am developing a JavaScript animation script and I am aiming to allow individuals to define behavior declaratively within the HTML. This is the format I envision for my HTML: <div data-animation="top: 600, left: 600, opacity: 1, start: 0.2 ...

Nuxt.js implemented with Auth using jwt refresh tokens

I've implemented the Auth library in my Vue/Nuxt project and have successfully set up JWT Authentication. However, I'm encountering an issue with the refresh token. The refreshToken cookie is consistently being set to null: Furthermore, when at ...

Having issues with the jQuery toggle functionality

var resultsList = $("#test"); resultsList.text("Hello. This is jQuery!"); var tB = jQuery("#toggleButton"); tB.on("click", function() { resultsList.toggle(400); }); The syntax appears to be correct as there are no errors reported in the browser cons ...

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 ...

Exploring TypeScript: Optional Sub-Properties

I've been exploring ways to create a type-alias with properties like "answer" that I came across in this post by another user (Typescript interface optional properties depending on other property). Here's an example: type Sample = { key1: true, ...

Issues arise when using Jquery events in conjunction with AngularJS causing them to function

I have encountered an issue with my AngularJS menu code. I am sending an array to prepare the menu when the document loads. In this sequence, I have also added a click event to the generated menu. However, the click event does not fire if it is placed befo ...