I want to insert some html asynchronously into the page, and then execute some javascript codes correlated with the inserted DOM.
This kind of endeavour always fail because DOM rendering in the browser takes much more time than the next javascript codes to execute.
I have met with such problem before and have asked a question, but nobody answered. It's here: https://stackoverflow.com/questions/31935005/whats-going-on-with-dom-after-orientationchange-event
So, can I get a promise or attach some callback function?
Codes here:
var bubble = function bubble(type,content){
var myScroll, temp = document.createElement('div');
temp.innerHTML=content;
temp.className=type==='time'?'time':'bubble '+type;
document.getElementsByClassName('dialogue')[0].appendChild(temp);
if(type==='reply')myScroll = new IScroll('.dlg-wrapper', { mouseWheel: true });
};
I don't think rendering is asynchronous, but it is triggered by a message that is only processed after the script ended.
But you said "and then execute some javascript codes correlated with the inserted DOM.". I don't see that happening in your code.
Anyway, if you use
setTimeout(callback, 0)you can do the post-processing in callback.setTimeoutis also controlled by that message queue, so after the code that modified the DOM is finished, the browser will first process the redraw message (because that is first in the message queue), and only then call the callback.