setinterval

Understanding JavaScript setTimeout and setInterval

ぐ巨炮叔叔 提交于 2019-12-11 03:20:18
问题 I need a bit of help understanding and learning how to control these functions to do what I intend for them to do So basically I'm coming from a Java background and diving into JavaScript with a "Pong game" project. I have managed to get the game running with setInteval calling my main game loop every 20ms, so that's all ok. However I'm trying to implement a "countdown-to-begin-round" type of feature that basically makes a hidden div visible between rounds, sets it's innerHTML = "3" // then

Is setInterval handler called while my script running?

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-11 03:00:08
问题 I planned to use setInterval to simply set a variable to false, which would be inspected by the main loop to stop. Example (note: this is an example only, the acutal code is not a while() loop which would be easy to reconstruct, but a quite complex, and long to execute script generated by a closed source software actually): var running = true; setInterval(function () { if (running) { console.log("Stopping now!"); running = false; } }, 100); while (running) { // do something ... } However it

Is there a need to prepend setTimeout and setInterval with window object?

家住魔仙堡 提交于 2019-12-11 02:45:33
问题 In the Mozilla documentation, there are some examples written with window. in front of the timer functions and some without: function delayedAlert() { timeoutID = window.setTimeout(slowAlert, 2000); }... setTimeout(myArray.myMethod, 1000);... window.setInterval = function (vCallback, nDelay... I have been writing my code without window. in front without any problem so far. I want to find out if there is any situation when it would be necessary. 回答1: If .. There is no other identifier in scope

setTimeout takes longer than it should

喜夏-厌秋 提交于 2019-12-11 02:29:04
问题 Could someone please explain why the script, below with the setTimeout command, takes a lot longer to execute (400-500 ms) in Greasemonkey than it does in the Firefox console, where it's exactly 100 ms? var start = new Date ().getTime (); console.log ( new Date ().getHours ()+" : " + new Date ().getMinutes () + " : " + new Date ().getSeconds () + " : " + new Date ().getMilliseconds () ); setTimeout (foo,100); //foo (); function foo () { var time = new Date () - start; console.log ("Execution

Why isn't clearInterval working in this code?

允我心安 提交于 2019-12-11 00:49:41
问题 There is a function which sets an interval using setInterval() , but even after calling clearInterval() , I can see in the console that the else condition is still running. How can I clear that interval properly? function increase(old, step, neu) { var i = 0; var delay2; function countUp() { if (i < 5) { old += step; // console.log("increase") $("#total-price-value").text(old + " dollors"); $("#total-price-value").digits(); i++; delay2 = setInterval(countUp, 80); } else { clearInterval(delay2

计时器 setInterval 练习, 秒杀 时钟

ⅰ亾dé卋堺 提交于 2019-12-10 21:14:12
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <meta http-equiv="X-UA-Compatible" content="ie=edge" /> <title>Date日期对象练习</title> <style> * { margin: 0; padding: 0px; } div { width: 100%; height: 950px; background-image: url(./2.jpg); background-size: 100%; background-repeat: no-repeat; text-align: center; line-height: 950px; } span { width: 150px; height: 150px; border: 3px solid #d4d1b2; background: rgba(149, 187, 226, 0.6); text-align: center; line-height: 150px; font-size: 80px; color: rgb(255, 255,

clearInterval outside of method containing setInterval

笑着哭i 提交于 2019-12-10 20:27:12
问题 I have a messaging function that can be called several times on the same page. Each time it's called, a new message appears at the top of the screen and has a unique ID set by an incrementing global. The general idea of the function is below: var messageTimer = function(msgid, duration) { ... var interval = setInterval( function() { ... }, duration ) }; In the code above, a new message is created and a timer applied. If the timer runs out, the message disappears. However, if the message is

how to replace setInterval by requestAnimationFrame

旧巷老猫 提交于 2019-12-10 20:25:30
问题 Here is my situation, I need to speed up the function running time, so setInterval is not a wise choose, right? Since it will cost at least 4ms for each time. So, may I change setInterval function to requestAnimationFrame, but I don't quite understand how the requestAnimationFrame works. For example // some code here var interval = setInterval(doSomething, 10) var progress = 0 function doSomething(){ if (progress != 100){ // do some thing here }else{ clearInterval(interval) } } and how can I

HTML5 + Jscript with JQuery, setInterval problem

天大地大妈咪最大 提交于 2019-12-10 20:14:53
问题 Please can someone tell me why this isn't working before I defenestrate everything on my desk. I have the following html document: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html lang="en"> <head> <meta http-equiv="Content-Type" content="text/html"; charset=utf-8 /> <title>Sample Gauge using JQuery</title> </head> <body> <canvas id="gauge" width="200" height="200"> Your web browser does not support HTML 5 because

setInterval doesn't slow down on inactive tab

天大地大妈咪最大 提交于 2019-12-10 18:59:15
问题 This is perhaps the opposite of a very common problem: When you use setInterval on one tab and you switch to another, the interval decreases significantly (i.e. slow down). This is very noticeable with audio timing since you can hear it being slowed down. But on the latest Chrome 56 and Firefox 51, I can not make it happen anymore. However on Chrome very rarely, and seemingly randomly, switching tabs can have an effect. And it even throttles Web Workers for some reason. This is causing me