LoginSignup
4
3

More than 5 years have passed since last update.

underscoreコードリーディング(defer)

Posted at

underscoreに詳しくないので、勉強半分でソースコードを読む。

利用するバージョン

underscore.js(v1.8.3)

deferとは

underscorejs.orgのdefer

こんな説明。

_.defer(function, *arguments)

Defers invoking the function until the current call stack has cleared, similar to using setTimeout with a delay of 0.
Useful for performing expensive computations or HTML rendering in chunks without blocking the UI thread from updating.
If you pass the optional arguments, they will be forwarded on to the function when it is invoked.

_.defer(function(){ alert('deferred'); });
// Returns from the function before the alert runs.


0の遅延でsetTimeoutを利用するのと同様で、現在のコールスタックがクリアされるまで関数の呼び出しを遅延させます。
高等な計算をする場合や、大量のhtmlレンダリングをUIスレッドのupdateをブロックせずに行う場合に使いやすいです。
もしargumentsを渡した場合、functionが呼び出される際に受け渡されます

underscore.

コード的にはこのあたり。

 // Defers a function, scheduling it to run after the current call stack has
  // cleared.
  _.defer = _.partial(_.delay, _, 1);

.particalと.delayを用いて1ミリセカンド遅らせる。

4
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
3