javascript - How to Write A Function That Appends an Item to the DOM and Delays the Next Tick? -


i found following question online:

write function takes object , appends dom, making events buffered until next tick? explain why useful?

here response:

function appendelement(element) {     settimeout(function() {         document.body.appendchild(element);     }, 0); } 

why did set interval zero?

according this article, setting timeout 0, delays events until next tick:

the execution of func goes event queue on nearest timer tick. note, that’s not immediately. no actions performed until next tick.

here's uncertain of:

  • is solution correct?
  • i cannot answer why approach beneficial

for reference, got question website listing 8 javascript interview questions.

i'd point out asking question own research , improvement , not part of code challenge, interview question, or homework assignment.

i think misunderstood question. read asking append element dom, delay further processing until next tick. therefore:

document.appendchild(element); settimeout(function() {     resumeprogramflowfromhere(); }, 0); // nothing here 

that's useful when want make sure there reflow/repaint before time-consuming operation takes place (to give users visual feedback). browsers force repaint in certain circumstances, when don't, technique can useful.

you can find more information here , here.


that's interpretation of question, find confusing too, because it's not clear mean events. , there other debatable questions on site, weirdest being:

what concept of “functions objects” , how affect variable scope?

that makes no sense me. okay, functions objects in javascript, , scopes related functions, distinct topics. fact functions objects has nothing scope.

so advice is, take interview questions grain of salt.


Comments

Popular posts from this blog

jquery - How can I dynamically add a browser tab? -

node.js - Getting the socket id,user id pair of a logged in user(s) -

keyboard - C++ GetAsyncKeyState alternative -