setImmediate() and setTimeout()

setImmediate method

Requests that a function be called when current or pending tasks are complete, such as events or screen updates.

Syntax

var retVal = window.setImmediate(handler, arguments);

Standards information

Parameters

handler [in]

Type: any

The function to be called.

arguments [in, optional]

Type: any

Arguments to be passed to the function.

Return value

Type:  Integer

A handle to the request.

Remarks

JavaScript operations run in the same thread as events, display updates, and other additional tasks. As a result, extended JavaScript operations (such as functions containing many lines of code) prevent additional tasks from being handled. In turn, this makes an application appear to be unresponsive because events (such as onclick or onkeypress) are not handled and the screen is not updated until the extended operation completes.

The setImmediate method schedules the function specified in the handler parameterto run after the current script block completes.If additional actions are pending when the current script block completes, they are processed before the handler function is called. This effectively creates a yield between the current script block and the handler function.

If you break extended operations into separate functions, you can use setImmediate to call each function in sequence. When you do this, setImmediate allows additional tasks to complete before calling each function in the sequence. In turn, this enables the application to respond to user input and to handle additional tasks in a predictable and responsive fashion.

For more information, see the setImmediate TestDrive demo.

Note  

Some developers use setTimeout (or setInterval) to create events that accomplish similar results; however, there are subtle, but important differences between the two techniques.

The setTimeout method is restricted to 250 requests per second on most systems. This means that setTimeout(0, handler) waits roughly 4ms before executing, even if no additional actions are pending. In contrast, setImmediate yields between each request, no matter how many requests are waiting to processed. If no additional actions are pending, setImmediate calls the handler function immediately.




转载于:https://www.cnblogs.com/puncha/archive/2012/11/26/3876969.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值