Search code examples
javascriptjqueryiframedom-eventssettimeout

Access main window setTimeoutID in same domain iFrame


I am trying to alert user for inactivity about 30 secs. So in my application I am setting timeout for alert.

Main window code:

<script>
    var timeoutID = setTimeout('alert("You are logged out due to inactivity.")', 30000);
</script>

iFrame :

<script>
  $(document).ajaxStop(function(){
     clearTimeout(timeoutID);
     var timeoutID = setTimeout('alert("You are logged out due to inactivity.")', 30000);
  });
</script>

Now, when I am in iFrame and doing something my main window alert still comes. Not sure how to reset the same timeoutID.


Solution

  • use

    top.timeoutID

    Use it also when setting the new timeout, otherwise you will not be able to access to the timeout when navigating away from the iframe.

    there is also a typo: fucntion