2

I have a script that closes an iframe after a click it. I need that when you click on the link inside the iframe has delay 15 seconds to load a page within the iframe and after 15 seconds the iframe automatically closes.

   $(window).on('blur',function(e) {    
    if($(this).data('mouseIn') != 'yes')return;
    $('iframe').filter(function(){
     return $(this).data('mouseIn') == 'yes';
    }).trigger('iframeclick');    
   });

   $(window).mouseenter(function(){
    $(this).data('mouseIn', 'yes');
   }).mouseleave(function(){
    $(this).data('mouseIn', 'no');
   });

   $('iframe').mouseenter(function(){
    $(this).data('mouseIn', 'yes');
    $(window).data('mouseIn', 'yes');
   }).mouseleave(function(){
    $(this).data('mouseIn', null);
   });

   $('iframe').on('iframeclick', function(){
                $(this).hide()
   });

   $('<input type="text" style="position:absolute;opacity:0;height:0px;width:0px;"/>').appendTo(document.body).blur(function(){
     $(window).trigger('blur');
    }).focus();
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<iframe style="width:400px;
height:250px;" src="//example.com"></iframe>
Murilo
  • 49
  • 9

5 Answers5

1

If you want to stick to Jquery you can use .delay()

http://api.jquery.com/delay/

To wait 15 seconds before the iframe close, you should do this :

$('iframe').on('iframeclick', function(){
                $(this).delay(15000).hide();
}

In a simple way, you put a delay of 15s before the "hide" action

0

You are looking for the setTimeout() method. You can find how to use it here: http://www.w3schools.com/jsref/met_win_settimeout.asp.

In your case would be something like (this can be optimized, take out global var etc.):

 globalVar = null;
 function closeIframe() {
     globalVar.hide();
 }
 $('iframe').on('iframeclick', function(){
      globalVar = $(this);
      setTimeout("closeIframe()", 15000);
 });
zozo
  • 8,230
  • 19
  • 79
  • 134
0

Use good old setTimeout:

var timeoutValue = 15000; /*ms*/
setTimeout(function(){...}, timeoutValue);

which in you case could look like:

var close = function(element){element.hide();};
$('iframe').on('iframeclick', function(){
  var timeoutValue = 15000;
  var context = $(this);
  setTimeout(function(){close(context)}, timeoutValue);
});
Mathias Vonende
  • 1,400
  • 1
  • 18
  • 28
0

Binding event listeners to elements inside the iframe will only work if both the parent site and the iframe are on the same domain. If the contents of the iframe is on a different domain, you won't be able to perform this as it is classed as "click-jacking", which is a big security threat. See http://en.wikipedia.org/wiki/Same-origin_policy

If the iframe is on the same domain then you can add the event listener inside the iframe and then call the parent to notify the event.

You can also grab the contents and find the element you want to bind the event handler on.

$("iframe#name").contents().find(".link").on("click", function() {
    $("iframe#name").delay(15000).hide();
});

Related:

Community
  • 1
  • 1
Miguel Mota
  • 20,135
  • 5
  • 45
  • 64
0

I used sleep () function and solved my problem!

Murilo
  • 49
  • 9