计时器在无限循环运行中不起作用

时间:2018-12-15 20:53:59

标签: javascript

我正在制作一个用于计算素数的非常基本的node.js应用程序,它使用无限循环连续计算素数,我想通过使用void update() { glm::vec3 front; front.x = cos(glm::radians(Yaw)) * cos(glm::radians(Pitch)); front.y = FrontGo.y; front.z = sin(glm::radians(Yaw)) * cos(glm::radians(Pitch)); FrontGo = glm::normalize(front); if (prewYaw > Yaw) { lastRotationDegree = glm::acos(glm::dot(FrontGo, PrevFrontGo) / (glm::length(FrontGo) + glm::length(PrevFrontGo))); } else { lastRotationDegree = -glm::acos(glm::dot(FrontGo, PrevFrontGo) / (glm::length(FrontGo) + glm::length(PrevFrontGo))); } PrevFrontGo = FrontGo; prewYaw = Yaw; glm::vec3 frontl; frontl.x = cos(glm::radians(Yaw)) * cos(glm::radians(Pitch)); frontl.y = sin(glm::radians(Pitch)); frontl.z = sin(glm::radians(Yaw)) * cos(glm::radians(Pitch)); FrontLook = glm::normalize(frontl); Right = glm::normalize(glm::cross(FrontLook, wUp)); Up = glm::normalize(glm::cross(Right,FrontLook)); } 和{{1}为每个素数添加一个时间码}变量,但是void ProcessKeyboardP(Player_Movement direction, float deltaTime) { float velocity = MovementSpeed * deltaTime; if (direction == FORWARD_P) Position += FrontGo * velocity; if (direction == BACKWARD_P) Position -= FrontGo * velocity; if (direction == LEFT_P) Position -= Right * velocity; if (direction == RIGHT_P) Position += Right * velocity; } 没有运行,我怀疑这是由于无限循环造成的,是否有任何修复/替代方法?这是我的代码:

setinterval

primecalc(primes)

1 个答案:

答案 0 :(得分:1)

考虑将您的代码放在具有较小超时的间隔中,而不是无限循环。循环会停止执行,让一个循环永远运行对您而言效果不佳。另外,正如@Bergi所指出的,在间隔的每次迭代上运行Date.now()会比跟踪单独的计时器更好。