You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
zhangzifa opened this issue
Jul 6, 2016
· 2 comments
Assignees
Labels
duplicateIssues and PRs that are duplicates of other issues or PRs.timersIssues and PRs related to the timers subsystem / setImmediate, setInterval, setTimeout.
This is an issue about if callback duration should be included in setInterval/setTimeout interval or not.
In my opinion, callback duration should NOT be included in interval.
Yes, we know JavaScript timer is not 100% accurate. However, it should be logically accurate, which is important and necessary, especially for the IoT applications.
setInterval/setTimeout is defined in ../lib/timer.js as:
Timer should follow these rules(repeat stands for both repeat and after):
If all the callbacks in a timer list can be executed within repeat, the real interval should be repeat.
If all the callbacks in a timer list executed longer than repeat, the real interval should be the real execute time (given other event almost takes little time to execute).
The above logic should work for timers with different repeat.
When using setInterval, the real interval is not as expected.
Node.js 4.x, 5.x and 6.x:
Real interval includes double duration of callback.
Node.js 0.10.x and 0.12.x
Real interval includes the duration of callback.
If you want to know the detailed test and result, please move to #7346, provided by @distributedlife .
From the discussion of #5426#3063, it's an known issue.
I raised another #7386. However, that fix is not mature.
How should setInterval behaves? [#3063] provides an port from 0.10.x and 0.12.x.
I think there is a better solution to make the real interval not include callback duraiton.
This fix works for #5426, too. I list the output at the end of this issue.
The following works as the above (code based on LTS 4.x):
diff--gita/lib/timers.jsb/lib/timers.jsindex3cd830a..0bfea18100644---a/lib/timers.js+++b/lib/timers.js
@@ -28,11+28,15 @@ varlists={};// it will reset its timeout.// the main function - creates lists on demand and the watchers associated// with them.-exports.active=function(item){+exports.active=function(item,start){constmsecs=item._idleTimeout;if(msecs<0||msecs===undefined)return;-item._idleStart=Timer.now();+if(start){+item._idleStart=start;+}else{+item._idleStart=Timer.now();+}varlist;
@@ -61,11+65,28 @@ functionlistOnTimeout(){debug('timeout callback %d',msecs);varnow=Timer.now();+varisFirstCalled=false;debug('now: %s',now);vardiff,first,threw;while(first=L.peek(list)){+now=Timer.now();diff=now-first._idleStart;++if(!isFirstCalled){+isFirstCalled=true;+first._firstCalled=true;+}else{+if(first._firstCalled){+first._firstCalled=false;+// it needs more than msecs+// when the callbacks of all the timers called+debug('%d list execute time is larger than %d',msecs,msecs);+list.start(0,0);+return;+}+}+if(diff<msecs){list.start(msecs-diff,0);debug('%d list wait because diff is %d',msecs,diff);
@@ -379,6+400,7 @@ exports.setInterval=function(callback,repeat){returntimer;functionwrapper(){+varnow=Timer.now();timer._repeat();// Timer might be closed - no point in restarting it
@@ -390,7+412,7 @@ exports.setInterval=function(callback,repeat){this._handle.start(repeat,0);}else{timer._idleTimeout=repeat;-exports.active(timer);+exports.active(timer,now);}}};
@@ -411,6+433,7 @@ constTimeout=function(after){this._idleNext=this;this._idleStart=null;this._onTimeout=null;+this._firstCalled=false;this._repeat=null;};
zhangzifa
changed the title
setInterval interval should not include duration of callback
setInterval/setTimeout interval should not include duration of callback
Jul 6, 2016
mscdex
added
the
timers
Issues and PRs related to the timers subsystem / setImmediate, setInterval, setTimeout.
label
Jul 6, 2016
I believe it would help if we kept these two issues separate, so please comment in the respective separate issues/PRs instead of creating new ones that conflate distinct problems.
duplicateIssues and PRs that are duplicates of other issues or PRs.timersIssues and PRs related to the timers subsystem / setImmediate, setInterval, setTimeout.
@Fishrock123 @misterdjules @whitlockjc @bnoordhuis
This is an issue about if callback duration should be included in setInterval/setTimeout interval or not.
In my opinion, callback duration should NOT be included in interval.
Yes, we know JavaScript timer is not 100% accurate. However, it should be logically accurate, which is important and necessary, especially for the IoT applications.
setInterval/setTimeout is defined in ../lib/timer.js as:
Timer should follow these rules(
repeat
stands for bothrepeat
andafter
):repeat
, the real interval should berepeat
.repeat
, the real interval should be the real execute time (given other event almost takes little time to execute).repeat
.When using setInterval, the real interval is not as expected.
If you want to know the detailed test and result, please move to #7346, provided by @distributedlife .
From the discussion of #5426 #3063, it's an known issue.
And has been fixed for over a year in v0.10.x and v0.12.x versions.
I raised another #7386. However, that
fix
is not mature.How should setInterval behaves? [#3063] provides an port from 0.10.x and 0.12.x.
I think there is a better solution to make the real interval not include callback duraiton.
This fix works for #5426, too. I list the output at the end of this issue.
The following works as the above (code based on LTS 4.x):
This is the test code:
This is the output:
validation for #5426
The text was updated successfully, but these errors were encountered: