为什么时间限制的循环在覆盆子pi上产生零迭代而在PC上产生数百万?

时间:2017-02-23 14:00:30

标签: javascript raspberry-pi

// set function to be called after 1 second
setTimeout(function() {
console.log('Timeout ran at ' + new Date().toTimeString());
}, 1000);

// store the start time
var start = new Date();
console.log('Enter loop at: '+start.toTimeString());

// run a loop for 4 seconds
var i = 0;
// increment i while (current time < start time + 4000 ms)
while((new Date().getTime()) < (start.getTime() + 4000)) {
  i++;
}

//print out the loop count
console.log('Exit loop at: '
        +new Date().toTimeString()
        +'. Ran '+i+' iterations.');

我在raspberryPi上尝试了上面的代码,结果在

下面
Enter loop at:Thu Feb 23 2017 13:30:57 GMT+0000 (UTC)
Exit loop at:13:30:57 GMT+0000 (UTC). Ran 0 iterations.
Time out atThu Feb 23 2017 13:30:58 GMT+0000 (UTC)

在教程中,结果是

Enter loop at: 20:04:50 GMT+0300 (EEST)
Exit loop at: 20:04:54 GMT+0300 (EEST). Ran 3622837 iterations.
Timeout ran at 20:04:54 GMT+0300 (EEST)

根据我的结果,它使用多线程进行处理,而在教程的结果中,它是一个单独的进程。我真的不知道现在哪一个是正确的。

0 个答案:

没有答案