I have a simple code. The setInterval()
should be running 60
times a second, yet console.log()
shows only 35
.
Also, this behavior happens on my client side. It appears whenever I use setInterval()
, it runs at half the speed.
I've also tried the same with setTimeout()
, which produces identical result.
Any ideas?
Node.js
const http = require('http'),
express = require('express'),
socketio = require('socket.io'),
process = require('process'),
config = require('./config.js');
const settings = {
cps: 60
};
var app = express();
var port = process.env.PORT || process.argv[2] || 8080;
var server = app.listen(port);
var io = socketio(server);
//Method 1
setInterval(() => {
checkCPS();
}, 16.67);
//Method 2
//function update(){
// checkCPS();
// setTimeout(function(){
// update();
// }, 1000/settings.cps);
//}
var cps = 0,
cpsCheck = 0,
cpsSave = 0;
function checkCPS(){
cps++;
var d = new Date().getSeconds();
if(cpsCheck != d){
cpsCheck = d;
cpsSave = cps;
cps = 0;
console.log(cpsSave);
}
}
update();
Another test code, still getting 34 cycles only
const http = require('http'),
express = require('express'),
socketio = require('socket.io'),
process = require('process'),
config = require('./config.js');
const settings = {
cps: 60
};
var app = express();
var port = process.env.PORT || process.argv[2] || 8080;
var server = app.listen(port);
var io = socketio(server);
var User = {};
var SOCKET_LIST = {};
var PLAYER_LIST = {};
//Update LOBBIES
//setInterval(() => {
// checkCPS();
//}, 16.67);
function update(){
setTimeout(function(){
checkTime();
update();
}, 0);
}
var saveTime = 0,
saveTime2 = 0,
cps = 0;
function checkTime(){
var d = new Date().getTime();
if(d > 16.67 + saveTime){
cps++;
saveTime = d;
if(d > saveTime2 + 1000){
saveTime2 = d;
console.log(cps);
cps = 0;
}
}
}
update();