Question

I have a hidden frame that Accepted message, but I do not have to send by clicking. I'm calling the function that performs and actions, including sending a message. Function settimeout not satisfied by their speed.

this.request = function (requestData) {
    $(function () {
        dataOrign = requestData;
        var iframe = $('iframe#postFrame');
        setTimeout(function () {
            var parseData = JSON.stringify(requestData);
            iframe[0].contentWindow.postMessage({ request: parseData }, '*');
        }, 1000);
    });
};

How can i send postMessage with minimal time? In this situation?

Était-ce utile?

La solution

For most browsers, you can listen for the load event (some example found here) for that iframe to do the postMessage.

E.g.

$('iframe#postFrame').on('load', function(e) { 
    // do postMessage here
});

However it appears that the setTimeout/setInterval method is unavoidable for older browsers (IE7/8).

UPDATE

If you would like to write it as a function, you could do this:

var request = function(requestData, $iframe) { 
    if ($iframe.length > 0 && $iframe.get(0).document.readyState === 'completed') { 
        // do postMessage
    }
};

Then you can just use it this way:

request(data, $('iframe#postFrame'));
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top