setinterval

Why does the setInterval callback execute only once?

假装没事ソ 提交于 2019-11-26 01:25:46
问题 I have this counter I made but I want it to run forever, it\'s really simple, what am I doing wrong here? function timer() { console.log(\"timer!\") } window.setInterval(timer(), 1000) 回答1: You used a function call instead of a function reference as the first parameter of the setInterval. Do it like this: function timer() { console.log("timer!"); } window.setInterval(timer, 1000); Or shorter (but when the function gets bigger also less readable): window.setInterval( function() { console.log(

Stop setInterval

空扰寡人 提交于 2019-11-26 00:59:36
问题 I want to stop this interval in the error handler from running repeatedly. Is that possible, and if so, how? // example code $(document).on(\'ready\',function(){ setInterval(updateDiv,3000); }); function updateDiv(){ $.ajax({ url: \'getContent.php\', success: function(data){ $(\'.square\').html(data); }, error: function(){ $.playSound(\'oneday.wav\'); $(\'.square\').html(\'<span style=\"color:red\">Connection problems</span>\'); // I want to stop it here } }); } 回答1: You need to set the

Code for a simple JavaScript countdown timer?

放肆的年华 提交于 2019-11-26 00:24:48
问题 I want to use a simple countdown timer starting at 30 seconds from when the function is run and ending at 0. No milliseconds. How can it be coded? 回答1: var count=30; var counter=setInterval(timer, 1000); //1000 will run it every 1 second function timer() { count=count-1; if (count <= 0) { clearInterval(counter); //counter ended, do something here return; } //Do code for showing the number of seconds here } To make the code for the timer appear in a paragraph (or anywhere else on the page),

Changing the interval of SetInterval while it&#39;s running

穿精又带淫゛_ 提交于 2019-11-26 00:14:12
问题 I have written a javascript function that uses setInterval to manipulate a string every tenth of a second for a certain number of iterations. function timer() { var section = document.getElementById(\'txt\').value; var len = section.length; var rands = new Array(); for (i=0; i<len; i++) { rands.push(Math.floor(Math.random()*len)); }; var counter = 0 var interval = setInterval(function() { var letters = section.split(\'\'); for (j=0; j < len; j++) { if (counter < rands[j]) { letters[j] = Math

Javascript setInterval and `this` solution

随声附和 提交于 2019-11-26 00:12:46
问题 I need to access this from my setInterval handler prefs: null, startup : function() { // init prefs ... this.retrieve_rate(); this.intervalID = setInterval(this.retrieve_rate, this.INTERVAL); }, retrieve_rate : function() { var ajax = null; ajax = new XMLHttpRequest(); ajax.open(\'GET\', \'http://xyz.com\', true); ajax.onload = function() { // access prefs here } } How can I access this.prefs in ajax.onload ? 回答1: The setInterval line should look like this:- this.intervalID = setInterval(

setTimeout or setInterval?

一曲冷凌霜 提交于 2019-11-25 23:56:29
问题 As far as I can tell, these two pieces of javascript behave the same way: Option A: function myTimeoutFunction() { doStuff(); setTimeout(myTimeoutFunction, 1000); } myTimeoutFunction(); Option B: function myTimeoutFunction() { doStuff(); } myTimeoutFunction(); setInterval(myTimeoutFunction, 1000); Is there any difference between using setTimeout and setInterval? 回答1: They essentially try to do the same thing, but the setInterval approach will be more accurate than the setTimeout approach,

Pass parameters in setInterval function

半世苍凉 提交于 2019-11-25 23:48:42
Please advise how to pass parameters into a function called using setInterval . My example setInterval(funca(10,3), 500); is incorrect. You need to create an anonymous function so the actual function isn't executed right away. setInterval( function() { funca(10,3); }, 500 ); sbr now with ES5, bind method Function prototype : setInterval(funca.bind(null,10,3),500); Reference here Add them as parameters to setInterval: setInterval(funca, 500, 10, 3); The syntax in your question uses eval, which is not recommended practice. You can pass the parameter(s) as a property of the function object, not

Execute the setInterval function without delay the first time

与世无争的帅哥 提交于 2019-11-25 23:46:00
问题 It\'s there a way to configure the setInterval method of javascript to execute the method immediately and then executes with the timer 回答1: It's simplest to just call the function yourself directly the first time: foo(); setInterval(foo, delay); However there are good reasons to avoid setInterval - in particular in some circumstances a whole load of setInterval events can arrive immediately after each other without any delay. Another reason is that if you want to stop the loop you have to

Calling a function every 60 seconds

百般思念 提交于 2019-11-25 23:21:14
问题 Using setTimeout() it is possible to launch a function at a specified time: setTimeout(function, 60000); But what if I would like to launch the function multiple times? Every time a time interval passes, I would like to execute the function (every 60 seconds, let\'s say). 回答1: If you don't care if the code within the timer may take longer than your interval, use setInterval() : setInterval(function, delay) That fires the function passed in as first parameter over and over. A better approach

How can I make setInterval also work when a tab is inactive in Chrome?

大兔子大兔子 提交于 2019-11-25 21:47:16
问题 I have a setInterval running a piece of code 30 times a second. This works great, however when I select another tab (so that the tab with my code becomes inactive), the setInterval is set to an idle state for some reason. I made this simplified test case (http://jsfiddle.net/7f6DX/3/): var $div = $(\'div\'); var a = 0; setInterval(function() { a++; $div.css(\"left\", a) }, 1000 / 30); If you run this code and then switch to another tab, wait a few seconds and go back, the animation continues