我有一个应用程序,该应用程序应该每1秒运行一次质数。

一切正常:控制台在开头说“开始”,在结尾说“结束”。但是,它不会在1秒后显示每个数字,而是一次显示所有内容。

index.js:

var prime = require('./prime');

prime.on('start', () => console.log("Event Started"));
prime.on('prime', () => { });
prime.on('end', () => console.log("Event Ended"));
prime.start();




prime.js:

var events = require('events');
var util = require('util');
util.inherits(PrimeNumber, events);
var prime = new PrimeNumber();
module.exports = prime;

function PrimeNumber() {
    events.call(this);
    var self = this;
    this.start = () => {
        self.emit('start');
        var interval = setInterval(() => {
            for (var counter = 2; counter <= 2000; counter++) {
                var notPrime = false;
                for (var i = 2; i <= counter; i++) {
                    if (counter % i === 0 && i !== counter) {
                        notPrime = true;
                    }
                }
                if (notPrime === false)
                    self.emit('prime', console.log(counter));
            }
            clearInterval(interval);
            self.emit('end');
        }, 1000);
    }
}


我已经尝试解决这一问题了几个小时,所以我们将不胜感激!

最佳答案

只需对代码进行最少的更改,就可以这样写

var events = require('events');
var util = require('util');
util.inherits(PrimeNumber, events);
var prime = new PrimeNumber();
module.exports = prime;

function PrimeNumber() {
    events.call(this);
    var self = this;
    var startAt = 1; // starting position - 1 for the outer loop for each interval
    this.start = () => {
        self.emit('start');
        var interval = setInterval(() => {
            for (var counter = startAt + 1; counter <= 2000; counter++) {
                var notPrime = false;
                for (var i = 2; i <= counter; i++) {
                    if (counter % i === 0 && i !== counter) {
                        notPrime = true;
                        break;
                    }
                }
                if (notPrime === false) {
                    console.log(counter)
                    self.emit('prime', counter);
                    startAt = counter; // save the current prime, so we can start the next interval at the right place
                    return; // don't fall through, because we found a prime
                }
            }
            // if we get here, we've reached 2001 without finding a prime
            clearInterval(interval);
            self.emit('end');
        }, 1000);
    }
}



  使用class进行更现代的处理-以及更简单的算法


const events = require('events');

class PrimeNumber extends events {
    constructor() {
        super();
    }
    start() {
        let primes = [2];
        this.emit('start');
        this.emit('prime', 2)
        let interval = setInterval(() => {
            for (let counter = primes[0] + 1; counter <= 2000; counter++) {
                if (primes.every(p => counter % p !== 0)) {
                    primes.unshift(counter);
                    this.emit('prime', counter);
                    return;
                }
            }
            clearInterval(interval);
            this.emit('end');
        }, 1000);
    }
}
const prime = new PrimeNumber();
module.exports = prime;

10-08 15:49