Delay windowopen inside click event handler

For several days I have been trying (without success) to open a new window with a delay without blocking Chrome in most cases. The latency is critical to my task because some animation has to take place before the window opens and I cannot let browsers block new tabs from opening. Here's an example from my code:

element.on("click", function(e){
    e.preventDefault();
    var self = $(this),
        url = self[0].href;

    window.setTimeout(function(){
        window.open(url);
    }, 1000);
});

      

Element

is a jQuery object (more precisely, an anchor element). I noticed that I have a better chance of success if I pass a string just like this

window.open("http://example.com");

      

I have tried this solution in Explorer (no problem), Chrome (problem), Firefox (no problem) and Opera (problem). I pass out because I have tried everything I can remember. Synchronous ajax calls, fake click firing, create fake anchor elements, and fake firing click event. Nothing works fine. Bonus question (in case anyone knows how to fix the problem): is there a way to call window.open anytime, anywhere from the code?

+3


source to share


2 answers


This works for me in Chrome http://jsbin.com/wuqopukayuwi/4/

var $element = $('#link');


$element.on('click', function () {

   var url= $(this).attr('href');

   window.setTimeout(function(){

     var windowObjRef = window.open(url);

     console.log(windowObjRef);

  }, 2000);

});

      



Things to check:

  • Obviously, double check that you have enabled popups for your domain in Chrome (it prompts you the first time it tries to open a new window

  • This is because Chrome expects to be able to access this window again programmatically, so you need to declare the response to this method as a variable. At least this is how I got started ...

0


source


As long as you know how long the animation runs, you sleep for a few seconds
Add sleep (), msleep () and usleep () to Node.js via C ++ bindings.

This is mostly useful for debugging.

These calls will block all JavaScript execution, stopping the Node.js event loop!



Using

`var sleep = require('sleep');
sleep.sleep(n): sleep for n seconds
sleep.msleep(n): sleep for n miliseconds
sleep.usleep(n): sleep for n microseconds (1 second is 1000000 
microseconds)`

      

0


source







All Articles