javascript 为什么下划线延迟解决了我的很多问题?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15489163/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 00:57:56  来源:igfitidea点击:

why does underscore defer fix so many of my issues?

javascriptbackbone.jsunderscore.js

提问by cesara

After using backbone for a couple of weeks I have realized that underscore defer ended up fixing many of the async issues I ran into regarding rendering various views. Can someone please help me understand exactly what underscore defer does and how is it different that $.ready() or other type of wait for dom to render functions. What are the down sides of using it ?

在使用主干几周后,我意识到下划线延迟最终解决了我在渲染各种视图时遇到的许多异步问题。有人可以帮助我准确理解下划线 defer 的作用,以及 $.ready() 或其他类型的等待 dom 呈现函数有什么不同。使用它的缺点是什么?

_.defer = function(func) {
    return _.delay.apply(_, [func, 1].concat(slice.call(arguments, 1)));
};

回答by Alex Wayne

# These are equivalent
_.defer(fn);
setTimeout(fn, 1);

So deferis simply a one millisecond setTimeout. (It's got a few more convenience features but those aren't important here.)

所以defer只是一毫秒setTimeout(它有一些更方便的功能,但这些在这里并不重要。)



JavaScript has run loops. It's single threaded, but its execution starts and stops based on events or timers. Each time your JS engine kicks on to run some code, it's starting one iteration of its run loop.

JavaScript 有运行循环。它是单线程的,但它的执行根据事件或计时器开始和停止。每次您的 JS 引擎开始运行一些代码时,它都会开始其运行循环的一次迭代。

So what deferdoes is say "run this code in the next run loop".

那么什么defer是说“在下一个运行循环中运行此代码”。

_.defer(function() { alert('after'); });
alert('before');

This alerts "before" and then "after". This is because the the current run loop concludes which alerts "before", and then right afterward a new run loop starts and runs the code the alerts "after".

这会在“之前”和“之后”发出警报。这是因为当前的运行循环结束了哪个警报“之前”,然后紧接着一个新的运行循环开始并运行警报“之后”的代码。

So anytime you have code right here, but you want it to run code which occurs after this code first, then you would use defer.

因此,只要您在这里有代码,但您希望它首先运行在此代码之后发生的代码,那么您就可以使用defer.

_.defer(functionToRunLast);
functionToRunFirst();

This can be handy with the DOM. Sometimes you change it, but the changes don't parse or render immediately. At the end of the run loop, the browser catches up and parses and renders the DOM, then the next run loop starts and can interact with the newly rendered DOM.

这对 DOM 来说很方便。有时您更改它,但更改不会立即解析或呈现。在 run loop 结束时,浏览器会追上并解析并渲染 DOM,然后下一个 run loop 开始并可以与新渲染的 DOM 进行交互。

(Exactly what scenarios cause this delayed DOM parse, I'm not sure, but I've noticed it in my own projects in the past.)

(究竟是什么场景导致了这种 DOM 解析延迟,我不确定,但我过去在我自己的项目中已经注意到了。)



It is NOTa replacement for DOM ready. The next run loop may happen beforeDOM ready ever fires, don't confuse these concepts.

不是DOM 就绪的替代品。下一个运行循环可能DOM ready 触发之前发生,不要混淆这些概念。