nodejs(五)同步异步--USING SETTIMEOUT INSTEAD OF SETINTERVAL TO FORCE SERIALIZATION
2013-08-24 16:08
645 查看
Let’s say you want a function that does some I/O — such as parsing a log fi le — that will periodically
be executed. Let’s give that function the generic name my_async_function. You could start by
using setInterval like this:
However, you must ensure that none of these functions execute at the same time. You really can’t
guarantee that if you are using setInterval. If my_async_function takes one millisecond longer
than the interval you are using, they would run at the same time.
You need to enforce that the interval between the end of one my_async_function and the start of
the next is the desired interval. Here is how you can do this:
Here you are declaring a function named schedule (line 3) and invoking it immediately after
it is declared (line 10). This schedule function schedules the do_it function to be executed in
one second (the chosen interval). After one second passes, this anonymous function fires, calling
my_async_function (line 5). When this function finishes, the anonymous callback you passed to it
is invoked (line 6), calling the schedule function, which will again schedule the do_it function to
be executed in one second, thus restarting the cycle.
be executed. Let’s give that function the generic name my_async_function. You could start by
using setInterval like this:
var interval = 1000; setInterval(function() { my_async_function(function() { console.log('my_async_function finished!'); }); },interval);
However, you must ensure that none of these functions execute at the same time. You really can’t
guarantee that if you are using setInterval. If my_async_function takes one millisecond longer
than the interval you are using, they would run at the same time.
You need to enforce that the interval between the end of one my_async_function and the start of
the next is the desired interval. Here is how you can do this:
var interval = 1000; // 1 second (function schedule() { setTimeout(function do_it() { my_async_function(function() { console.log('async is done!'); schedule(); //再次调用 }); }, interval); }());
Here you are declaring a function named schedule (line 3) and invoking it immediately after
it is declared (line 10). This schedule function schedules the do_it function to be executed in
one second (the chosen interval). After one second passes, this anonymous function fires, calling
my_async_function (line 5). When this function finishes, the anonymous callback you passed to it
is invoked (line 6), calling the schedule function, which will again schedule the do_it function to
be executed in one second, thus restarting the cycle.
相关文章推荐
- Use setInterval instead of setTimeout
- 关于http客户端常见错误"警告:Going to buffer response body of large or unknown size. Using getResponseBodyAsStream instead is recommended.
- 理解JavaScript 执行机制及异步回调(setTimeout/setInterval/Promise)
- How to sort an arbitrarily large set of data using Hadoop?
- Java JDBC - How to connect to Oracle using Service Name instead of SID
- How do I perform a manual full refresh instead of using the Apply program to do the full refresh?
- An example of using libexif to set JPEG Orientation Tag
- Using Interval Estimation to set prior value in Online Learning method
- How do I get ASP.NET Web API to return JSON instead of XML using Chrome
- Java-httpClient警告: Going to buffer response body of large or unknown size. Using getResponseBodyAsStream instead is recommended.
- Going to buffer response body of large or unknown size. Using getResponseBodyAsStream instead is rec
- 关于http客户端常见错误"警告:Going to buffer response body of large or unknown size. Using getResponseBodyAsStream instead is rec"
- An example of using libexif to set JPEG Orientation Tag
- 如何解决It's likely you're attempting to run Eclipse using the JRE instead of the JDK
- Going to buffer response body of large or unknown size. Using getResponseBodyAsStream instead is rec
- Using HashTable to realize a set which Provide a collection that contains a group of unique objects
- How to set timeout for user when login using zend_auth?
- attempting to bokeyaunrun eclipse useing the jre instead of jdk,to run eclipse using
- Going to buffer response body of large or unknown size. Using getResponseBodyAsStream instead is rec
- 170622 Save plot to image file instead of displaying it using Matplotlib