简体   繁体   English

浏览器不等待特定图像加载

[英]Browser not waiting for specific image to load

I have some logic that takes longer in some browsers than others ( cough IE cough ).我有一些逻辑在某些浏览器中比其他浏览器需要更长的时间(咳嗽IE咳嗽)。

So to improve UI feedback I'm trying to implement a loading spinner while the logic is being executed:因此,为了改善 UI 反馈,我尝试在执行逻辑时实现加载微调器:

function generatePlaceholderbuttons() {
    var spinner = new Image();
    spinner.id = "spinner";
    spinner.addEventListener("load", function () {
        try {
            var emailForm = new EmailForm();
            emailForm.dynamicSQLToButtons();
        }
        finally {
            spinner.parentElement.removeChild(spinner);
        }
    });
    document.getElementById("dynamic-sql-textarea").parentElement.parentElement.appendChild(spinner);
    spinner.src = "../Content/spinner.gif";
}

Chrome kind of works, in that it seems to display the spinner some times , whereas in IE 11 the DOM just freezes while the long running logic is being executed, and then appears to display the spinner. Chrome 的作品,因为它似乎显示微调器有时,而在 IE 11 中,DOM 只是在执行长时间运行的逻辑时冻结,然后似乎显示微调器。

I haven't tried promises yet, because I'd ideally like to support older browsers.我还没有尝试过承诺,因为理想情况下我希望支持旧版浏览器。

EDIT:编辑:

So this does work, but this can't be the only way surely!所以这确实有效,但这肯定不是唯一的方法!

function generatePlaceholderbuttons(spinner) {
    spinner.style.visibility = "visible";
    setTimeout(function () {
        try {
            var emailForm = new EmailForm();
            emailForm.dynamicSQLToButtons();
        }
        finally {
            spinner.style.visibility = "hidden";
        }
    }, 1000);
}

dynamicSQLToButtons appears to be synchronous, which is the reason of your slower browsers hanging for a long while. dynamicSQLToButtons似乎是同步的,这是您较慢的浏览器长时间挂起的原因。 Convert this function so it will be asynchronous and use a callback, like将此 function 转换为异步并使用回调,例如

    try {
        var emailForm = new EmailForm();
        emailForm.dynamicSQLToButtons(function() {
            spinner.parentElement.removeChild(spinner);
        });
    }
    finally {
        //spinner.parentElement.removeChild(spinner);
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM