{"action":"created","issue":{"url":"https://api.github.com/repos/nodejs/node/issues/7386","repository_url":"https://api.github.com/repos/nodejs/node","labels_url":"https://api.github.com/repos/nodejs/node/issues/7386/labels{/name}","comments_url":"https://api.github.com/repos/nodejs/node/issues/7386/comments","events_url":"https://api.github.com/repos/nodejs/node/issues/7386/events","html_url":"https://github.com/nodejs/node/issues/7386","id":161885222,"number":7386,"title":"Interval set by setInterval is unreasonable","user":{"login":"zhangzifa","id":7731478,"avatar_url":"https://avatars.githubusercontent.com/u/7731478?v=3","gravatar_id":"","url":"https://api.github.com/users/zhangzifa","html_url":"https://github.com/zhangzifa","followers_url":"https://api.github.com/users/zhangzifa/followers","following_url":"https://api.github.com/users/zhangzifa/following{/other_user}","gists_url":"https://api.github.com/users/zhangzifa/gists{/gist_id}","starred_url":"https://api.github.com/users/zhangzifa/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/zhangzifa/subscriptions","organizations_url":"https://api.github.com/users/zhangzifa/orgs","repos_url":"https://api.github.com/users/zhangzifa/repos","events_url":"https://api.github.com/users/zhangzifa/events{/privacy}","received_events_url":"https://api.github.com/users/zhangzifa/received_events","type":"User","site_admin":false},"labels":[{"url":"https://api.github.com/repos/nodejs/node/labels/timers","name":"timers","color":"f7c6c7"}],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":2,"created_at":"2016-06-23T10:03:31Z","updated_at":"2016-06-24T04:50:14Z","closed_at":null,"body":"<!--\r\nThank you for reporting an issue. Please fill in the template below. If unsure\r\nabout something, just do as best as you're able.\r\n\r\nVersion: usually output of `node -v`\r\nPlatform: either `uname -a` output, or if Windows, version and 32 or 64-bit\r\nSubsystem: if known, please specify affected core module name\r\n\r\nIf possible, please provide code that demonstrates the problem, keeping it as\r\nsimple and free of external dependencies as you are able.\r\n-->\r\n\r\n* **Version**: v4.4.5\r\n* **Platform**: Linux u 3.19.0-15-generic #15-Ubuntu SMP\r\n* **Subsystem**: Timer\r\n\r\n<!-- Enter your issue details below this comment. -->\r\nWhen set a timer by function `setInterval` with a certain interval (e.g 1000ms) , the execute time of the callback is another value (e.g 500ms). \r\n\r\nThe real interval between two callbacks is 1000 + 500 + 500ms.\r\n\r\nIt is more reasonable that the real interval is 1000 + 500ms.\r\n\r\nCode example:\r\n\r\n```\r\n// function sleep mocks some real logic that executed cost a certain time. \r\nconst sleep = function (milliSeconds) {\r\n    var startTime = new Date().getTime();\r\n    while (new Date().getTime() < startTime + milliSeconds);\r\n };\r\n\r\nconst start = new Date();\r\nvar last = start;\r\nvar interval = setInterval(function() {\r\n  var now = new Date();\r\n  console.log('timer callback from start  ' + (now - start) + 'ms ' + 'from last callback ' + (now - last) + 'ms');\r\n  last = now;\r\n  sleep(500);\r\n}, 1000);\r\n```\r\n\r\n\r\n\r\nThe output is something like this:\r\n\r\n```\r\ntimer callback from start  1537ms from last callback 1537ms\r\ntimer callback from start  3540ms from last callback 2003ms\r\ntimer callback from start  5543ms from last callback 2003ms\r\ntimer callback from start  7545ms from last callback 2002ms\r\ntimer callback from start  9546ms from last callback 2001ms\r\ntimer callback from start  11548ms from last callback 2002ms\r\ntimer callback from start  13549ms from last callback 2001ms\r\ntimer callback from start  15550ms from last callback 2001ms\r\n```\r\n\r\nWhen change the logic of listOnTimeout:\r\n\r\n```\r\nfunction listOnTimeout() {\r\n  var msecs = this.msecs;\r\n  var list = this;\r\n\r\n  debug('timeout callback %d', msecs);\r\n\r\n  var now = Timer.now();\r\n  debug('now: %s', now);\r\n\r\n  var diff, first, threw;\r\n  while (first = L.peek(list)) {\r\n    diff = now - first._idleStart;\r\n    if (diff < msecs) {\r\n      list.start(msecs - diff, 0);  =====>>>>>  list.start(msecs, 0);\r\n      debug('%d list wait because diff is %d', msecs, diff);\r\n      return;\r\n    } else {\r\n  ...\r\n\r\n```\r\n\r\nThe output of the code example:\r\n\r\n```\r\ntimer callback from start  1703ms from last callback 1703ms\r\ntimer callback from start  3207ms from last callback 1504ms\r\ntimer callback from start  4707ms from last callback 1500ms\r\ntimer callback from start  6207ms from last callback 1500ms\r\ntimer callback from start  7708ms from last callback 1501ms\r\ntimer callback from start  9210ms from last callback 1502ms\r\ntimer callback from start  10711ms from last callback 1501ms\r\n```\r\n\r\n\r\n"},"comment":{"url":"https://api.github.com/repos/nodejs/node/issues/comments/228257544","html_url":"https://github.com/nodejs/node/issues/7386#issuecomment-228257544","issue_url":"https://api.github.com/repos/nodejs/node/issues/7386","id":228257544,"user":{"login":"zhangzifa","id":7731478,"avatar_url":"https://avatars.githubusercontent.com/u/7731478?v=3","gravatar_id":"","url":"https://api.github.com/users/zhangzifa","html_url":"https://github.com/zhangzifa","followers_url":"https://api.github.com/users/zhangzifa/followers","following_url":"https://api.github.com/users/zhangzifa/following{/other_user}","gists_url":"https://api.github.com/users/zhangzifa/gists{/gist_id}","starred_url":"https://api.github.com/users/zhangzifa/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/zhangzifa/subscriptions","organizations_url":"https://api.github.com/users/zhangzifa/orgs","repos_url":"https://api.github.com/users/zhangzifa/repos","events_url":"https://api.github.com/users/zhangzifa/events{/privacy}","received_events_url":"https://api.github.com/users/zhangzifa/received_events","type":"User","site_admin":false},"created_at":"2016-06-24T04:50:13Z","updated_at":"2016-06-24T04:50:13Z","body":"Yes, it is.\r\n\r\n\r\n"}}
{"actor":{"display_login":"zhangzifa"}}