窗口加载未在承诺回调中触发 [英] Window load not firing in promise callback

查看:43
本文介绍了窗口加载未在承诺回调中触发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是我试图实现的基本示例.我在 chrome/FF 上有这个工作,但 IE 拒绝玩得很好.

The following is avery basic example of what im trying to achieve. I have this working in chrome/FF but IE refuses to play nice.

somePromiseThatFetchesData.then((data)=>{

    $(document).ready(function(){
        var markup = functionThatGeneratesMarkup(data);
        $('body').html(markup);
    });

    $(window).on('load', function(){
        alert('all images etc from promise loaded finishes loading')
    });

});

我的目标是用获取的数据填充我的 dom,然后等到我生成的数据中的资产加载完毕再触发另一个函数.问题是我的回调中的 alert 永远不会在 IE

The goal for me is to populate my dom with the fetched data, then wait until the assets in my generated data have loaded before firing another function. The problem is the alert in my callback never gets fired in IE

我在 IE 中使用 es6-promise polyfill

Im using the es6-promise polyfill for IE

推荐答案

您没有使用好的策略.如果 load 事件在您附加事件处理程序之前已经触发,则处理程序将永远不会执行.

You're not using a good strategy. If the load event has already fired before you attach the event handler, the handler will never execute.

相反,立即附加事件,然后将您的承诺处理程序放入其中:

Instead, attach the events right away and then have your promise handlers inside them:

$(document).ready(function(){
    somePromiseThatFetchesData.then((data)=>{
        var markup = functionThatGeneratesMarkup(data);
        $('body').html(markup);
    });
});

$(window).on('load', function(){
    somePromiseThatFetchesData.then((data)=>{
        alert('all images etc from promise loaded finishes loading')
    });
});

这篇关于窗口加载未在承诺回调中触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆