One solution would be to check if the iframe has focus and then does not stop the timer. eg.
$(window).blur(function () { // check focus if ($('iframe').is(':focus')) { // dont stop countdown } else { // stop countdown } });
Now this will work, however, if your iframe has focus, when the user changes the tab, the countdown will not stop. Therefore, in this situation, you will need to think of an elegant solution to move the focus from the previous iframe. For example, if the user clicks inside the iframe, immanently moves the focus back to the parent window.
Edit - Updated answer to include additional iframe features
Ok, so I played with that. Now I donβt know what content you have in your iframe, but you can add some code to it that basically sends focus back to the object in the parent window when clicked. eg.
In iFrame
<script> $(function () { $(document).click(function () { </script>
On the home page
<script> function setFocus() { // focus on an element on your page. $('an-element-on-your-page').focus(); } $(function () { $(window).focus(function (e) { // bind the blur event setBindingEvent(); }); var setBindingEvent = function () { // unbind $(window).unbind('blur'); $(window).blur(function () { // check focus if ($('iframe').is(':focus')) { // unbind the blur event $(window).unbind('blur'); } else { // stop countdown } }); }; setBindingEvent(); }); </script>
This will allow you to click on the iframe, set focus to the main page, and then stop the countdown.
source share