setinterval

setInterval going too fast

蓝咒 提交于 2019-12-20 03:38:33
问题 I'm new to JS, and decided to start of learning by a making a small game. I am using a setInterval to automate the enemy's attack. For their first attack the interval is correct, but after the second attack it speeds up to attacking almost three times, or more, a second. I'm also having trouble stopping the interval once either the player's or the enemy's health reaches 0. here is pretty much all the code pertaining my problem. The whole code can be found here function deadFunct(){ if

Typescript: private member is suddenly undefined

北战南征 提交于 2019-12-20 03:38:09
问题 So, I've got a basic Typescript application, that shouldn't actually cause any major problems, but it seems, that something's going wrong here and I have no idea what. I do have this private member minUpdateRate in my GameContainer class, that is initialized in the constructor. This seems to go well, because when GameContainer.start() is called, the console.log() method will print out 1 . However, when the GameContainer.render() method is called, it seems to be out of the scope or something,

JS class method in a setInterval doesn't work [duplicate]

亡梦爱人 提交于 2019-12-20 03:26:23
问题 This question already has answers here : Javascript setInterval and `this` solution (9 answers) Closed last year . I have this simple example with a class with a setInterval that calls main() every 5 seconds. When it comes to call print() it returns me TypeError: this.print is not a function. And I'm really stuck. Why if I call main() without setInterval it works smoothly but with setInterval it fails? It's weird. Any workaround to call main() periodically without this issue? "use strict";

Memory Leak with an XMLHttpRequest and setInterval

戏子无情 提交于 2019-12-20 02:38:17
问题 Here's some code that I run on Google Chrome 19.0.1061.1 (Official Build 125213) dev: <html> <title>Memory Leak</title> <script type="text/javascript"> (function(){ this.window.setInterval(function() { var xhr = new XMLHttpRequest(); xhr.open('GET', '', false); xhr.send(); }, 50); }).call(this); </script> </html> When I inspect memory usage in chrome://tasks, I can see that "Private Memory" is growing up indefinitely (8GB RAM config). If I change the sample of code above to something like

Recursive setInterval() runs continuously

旧城冷巷雨未停 提交于 2019-12-19 17:35:20
问题 I'm trying to run a function every 5 seconds using JavaScript using a recursive setInterval function. The following code just logs "started" as fast as possible and then crashes the browser. Why is this not running every 5 seconds? function five() { console.log("five"); setInterval(five(), 5000); } five(); 回答1: Don't use setInterval this way. Use setTimeout. By calling setInterval, you create a UNIQUE timer every time the function is called. SetTimeout would create one timer that ends, and

how to animate jquery load()

為{幸葍}努か 提交于 2019-12-19 16:15:26
问题 hello im using this code to load content from another php file. $(document).ready(function(){ setInterval(function(){ $('.live-stream ul').each(function(){ $(this).load('tx.php'); }); }, 1000); }); this works correctly but i want script to fadeIn each "li" when a new record added, anyone? the thing i want to do is something like facebook's live user action feed that on the right top of facebook home 回答1: You have to hide it first. $(this).hide().load("tx.php").fadeIn('500'); 回答2: Try

浅谈setTimeout和setInterval

∥☆過路亽.° 提交于 2019-12-19 15:31:07
最近阅读了相关setTimeout和setInterval的相关文档,受益匪浅,在这里贴出链接分享。 首先是这篇腾讯alloyteam团队的文章,关于setTimeout的事件循环机制讲的非常清楚。 http://www.alloyteam.com/2015/10/turning-to-javascript-series-from-settimeout-said-the-event-loop-model/ 然后是国外的一片读物: http://ejohn.org/blog/how-javascript-timers-work/ 中文翻译: https://segmentfault.com/a/1190000002633108 接下来是一些自己的理解。不正确之处还望指正。        setInterval(function(){ //need long time },500) 当回调函数执行需要很长的时间时,不论有没有执行完,到了500ms,都会向任务队列中又加入这个回调函数。也就是说如果回调函数执行需要2s中,那么在执行完这个回调函数中,任务队列中会新增4个执行回调函数的任务,当然每个任务执行又需要花费2s,这样又会多出4个,这样就会造成任务堆叠。所以在下面代码中你可以看到上一个回调任务结束时间基本同下一个回调任务开始时间一致。        var t=setInterval

google maps api real time updates with new geojson files

纵然是瞬间 提交于 2019-12-19 11:25:07
问题 I would like to add the real time dynamic to my map, by means of passing a new local geojson file about every 3 minutes. What is the best approach to do this so that it is seamless to the end-user. I am thinking ajax and or setinterval function is the answer, but I'm not sure where to begin. If anyone knows of any examples or can offer some advice. I would greatly appreciate it. Thank you. Here is my attempt to use AJAX. I am unable to loop over the geoJSON structure. Not sure what i ma doing

Closure for setInterval function in javascript

一个人想着一个人 提交于 2019-12-19 09:54:14
问题 How to use setInterval without using global variables? I'd prefer to wrap all variables of function invoked by setInerval in some kind of closure, like so: var wrap = function (f){ var local1, local2, ...; return function () { return f(); } } This doesn't work, but the idea is that I'd pass wrap(f) instead of f to setInterval , so that locals for f are nicely wrapped and don't pollute the global scope. 回答1: javascript don't have dynamic binding.(except this keyword) use anonymous function can

Javascript session timeout with popup alert for multiple tabs

可紊 提交于 2019-12-19 09:23:34
问题 I am using javascript setInterval() to check user idle time and show a popup alert before automatic logout. But it does not work for multiple tabs (working fine for single tab) Following is my code : localStorage.removeItem("idleTimeValue"); var idleInterval = setInterval(timerIncrement, 1000); function timerIncrement() { if(localStorage.getItem("idleTimeValue")) { idleTime = parseInt(localStorage.getItem("idleTimeValue")) + 1; //increments idle time by one second } else { idleTime = 1; }