简体   繁体   中英

Date.getTime not advancing in callbacks

So, I'm writing a page-scraper in Node and am getting odd behavior from Date.getTime in a set of callbacks.

function projectScrape(urlList){
        urlList.forEach(function(frag){
                request(frag.url, (function(frag){
                        return function(err, resp, body){
                                if(err) console.log('error: ' + err);
                                project$ = cheerio.load(body);
                                var tempRecord = {
                                        name: frag.name,
                                        funding: project$('span.monthly_funding_goal_percentage').text($
                                        subs: project$('span.number_of_subscribers').text(),
                                        timestamp: myDate.getTime()
                                        };
                                console.log(tempRecord);
                        }
                })(frag));
        });
};

The scrape works fine and I get a series of console dumps from the website. However, the timestamp on all of them are identical. The callbacks are clearly not finishing at the same time (sometimes up to a couple seconds between callback responses) - so why are they timestamped to the same millisecond?

Am I missing something about the function scoping here? As I see it, even if all of the callbacks are referencing the same instance of Date.getTime(), the console dumps should freeze the timestamps to when the individual callbacks return.

The only explanation that I can think of is that the Date.getTime() value is being stored when the callbacks are created and not updated when they actually fire.

Can anyone shed some light here?

Use Date.now() if you want the current timestamp, not .getTime() on a fixed date. Unless you modify myDate in any way it will always refer to the same time and date, thus .getTime() will always return the same value:

var tempRecord = {
  name: frag.name,
  funding: project$('span.monthly_funding_goal_percentage').text(/* ... */),
  subs: project$('span.number_of_subscribers').text(),
  timestamp: Date.now() // <---------
};

Don't use (new Date()).getTime() or var myTempDate = new Date(); return myTempDate.getTime() var myTempDate = new Date(); return myTempDate.getTime() because those will create new objects. You don't need them and they might even slow down your application (depends on the GC implementation).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM