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'));