找回密码
 注册

QQ登录

只需一步,快速开始

查看: 226|回复: 0

jQuery asynchronous function call, no AJAX request

[复制链接]
发表于 2013-10-23 20:35:51 | 显示全部楼层 |阅读模式
stockoverflow This post by John Resig (of jQuery) explains a bit about how JavaScript handles its single thread.  http://ejohn.org/blog/how-javascript-timers-work/

This article also explains: "One thing to remember when executing functions asynchronously in JavaScript, is all other JavaScript execution in the page halts until a function call is completed. This is how all the current browsers execute JavaScript, and can cause real performance issues if you are trying to call too many things asynchronously at the same time. A long running function will actually "lock up" the browser for the user. The same is true for synchronous function calls too."

All that said, it's possible you could simulate an asynchronous function call yourself by breaking whatever loop you are doing into a smaller chunk and using setTimeout().

For instance this function:
  1. // Sync
  2. (function() {
  3.   for(var x = 0; x < 100000; ++x) {console.log(x)}
  4. })()

  5. // ASync
  6. var x = 0;
  7. setTimeout(function() {
  8.    console.log(x++);
  9.    if(x < 100000) setTimeout(arguments.callee, 1);
  10. } ,1)
复制代码

You might want web workers!
Edit: I'm surprised how many people jumped to "its not possible" so I will elaborate. Web Workers are part of the HTML 5 spec. They allow you to spawn threads to run scripts in the background without blocking the UI. They must be external js files, are invoked by
var worker = new Worker('my_task.js');and are communicated to via events.

workers don't access the DOM because the DOM is not multi-thread safe. So the worker should run hardcore calculations and let the caller manipulate the DOM.       

From: stockoverflow

您需要登录后才可以回帖 登录 | 注册

本版积分规则

手机版|小黑屋|BC Morning Website ( Best Deal Inc. 001 )

GMT-8, 2026-6-11 03:06 , Processed in 0.016164 second(s), 16 queries .

Supported by Weloment Group X3.5

© 2008-2026 Best Deal Online

快速回复 返回顶部 返回列表