setinterval

How do I correctly use setInterval and clearInterval to switch between two different functions?

ぃ、小莉子 提交于 2019-11-27 08:40:37
For practice I am trying to display a number that increments from 0 - 9, then decrements from 9 - 0, and infinitely repeats. The code that I have so far seems to be close, but upon the second iteration the setInterval calls of my 2 respective functions countUp and countDown seem to be conflicting with each other, as the numbers displayed are not counting in the intended order... and then the browser crashes. Here is my code: <!DOCTYPE html> <html> <head> <title>Algorithm Test</title> </head> <body onload = "onloadFunctions();"> <script type = "text/javascript"> function onloadFunctions() {

setTimeout/setInterval 1000ms lag in background tabs (Chrome and Firefox)

点点圈 提交于 2019-11-27 06:15:17
问题 When events are queued with setTimeout/setInterval, and the user is viewing a separate tab, Chrome and Firefox enforce a minimum 1000ms lag before the event is executed. This article details the behaviour. This has been discussed on StackOverflow previously, but the questions and answers only applied to animations. Obviously, an animation can just be forced to update to the latest state when a user re-enters the tab. But the solution does not work for sequenced audio. I have Web Audio API

JavaScript - jQuery interval

ぃ、小莉子 提交于 2019-11-27 05:58:32
问题 I am using JavaScript with jQuery. I have the following script to alert hi every 30 seconds. $(document).ready( function() { alert("hi"); setInterval(function() { alert("hi"); }, 30000); }); I want to alert hi when the page loads (when document / page gets completely loaded) and on every 30 seconds interval afterwards (like hi (0s) - hi (30s) - hi (60s).. etc). But my solution works on two instances. One on DOM ready and the other in a loop. Is there any way to do the same in a single

setInterval() only running function once

随声附和 提交于 2019-11-27 05:11:28
I want to periodically query a PHP script for new messages. To do so, I'm using the setInterval() function and AJAX. $(document).ready(function(){ var queryInterval = 1000; /* How fast we query for new messages */ setInterval(getMessages(), queryInterval); function getMessages() { console.log("tick"); } }); However, when I look at the Javascript console, I'm only seeing "tick" once. I've made sure that the console doesn't ignore any more logs of the same strings, so if the code was working properly it should show "tick" in the console every second. Anyone know what could be going wrong here?

setInterval not working properly on Chrome

断了今生、忘了曾经 提交于 2019-11-27 04:26:48
I have a custom made slideshow object to perform the usual stuff the name indicates on a website. It all works well except when I switch tabs in Chrome and come back to the website tab. When that happens, the slideshow goes nuts and starts fading the images disregarding the setInterval interval given. Can't find anyhing related to this, so I'd like to at least know if it's a problem with the code or a software issue. Here's the code (being used with jQuery) : $(function() { // slideshow var slideshow = { id : false, current : 0, count : 0, interval : false, init : function(data) { if (!data)

setInterval with loop time

怎甘沉沦 提交于 2019-11-27 04:25:47
问题 setInterval(function(){}, 200) this code run the function each 200 miliseconds, how do I do it if I only want the function to be ran 10 times. thanks for help. 回答1: Use a counter which increments each time the callback gets executed, and when it reaches your desired number of executions, use clearInterval() to kill the timer: var counter = 0; var i = setInterval(function(){ // do your thing counter++; if(counter === 10) { clearInterval(i); } }, 200); 回答2: (function(){ var i = 10; (function k(

setInterval not working (firing only once) in Google Chrome extension

自作多情 提交于 2019-11-27 03:53:35
问题 Just as the title says: setInterval is only firing its callback once. manifest.json: { //... "content_scripts" : [{ "js" : ["code.js"], //... }], //... } code.js (example): setInterval(alert('only shown once'),2000); Why, and how I could fix it? The code works well outside of an extension (even in a bookmarklet). 回答1: setInterval(function() { alert('only shown once') },2000); You need to pass a function reference like alert and not a return value alert() 回答2: setInterval isn't working at all.

Is setInterval CPU intensive?

半腔热情 提交于 2019-11-27 03:52:50
I read somewhere that setInterval is CPU intensive. I created a script that uses setInterval and monitored the CPU usage but didn't notice a change. I want to know if there is something I missed. What the code does is check for changes to the hash in the URL (content after #) every 100 milliseconds and if it has changed, load a page using AJAX. If it has not changed, nothing happens. Would there be any CPU issues with that. I don't think setInterval is inherently going to cause you significant performance problems. I suspect the reputation may come from an earlier era, when CPUs were less

setInterval pauses in iphone/ipad (mobile Safari) during scrolling

我的未来我决定 提交于 2019-11-27 02:29:51
问题 I use the setInterval function in a website, and it works fine in IE, Chrome, Firefox and Safari. When i try it on ipad/iphone (safari mobile) i get problem: if i scroll the screen, the setInterval function pauses and it resumes only when i stop scrolling! is there a way to prevent the function from pausing? Thanks 回答1: I'm afraid no, there's no way to prevent such behaviour. There's a plenty of topics here in SO about this problem (more-o-less related), here's a particularly interesting one.

使用JavaScript让网页title动起来

六月ゝ 毕业季﹏ 提交于 2019-11-27 01:53:13
使用webQQ有些时间了,webQQ每次收到信息,就会看到title提示那个网友或群来信息,发现挺有意思,其实这个很简单。 HTML Title: 1: < title > 你好,Mr-S.R Lee </ title > JavaScript: 1: <script type= "text/javascript" > 2: function scroll() { 3: //获取title信息。 4: var titleInfo = document.title; 5: //获取title第一个汉字(数字、字母)。 6: //注释:字符串中第一个字符的下标是 0。如果参数 index 不在 0 与 string.length 之间,该方法将返回一个空字符串。 7: var firstInfo = titleInfo.charAt(0); 8: //获取第二位到最后的信息。 9: var lastInfo = titleInfo.substring(1, titleInfo.length); 10: //拼接输出信息 11: document.title = lastInfo + firstInfo; 12: } 13: //使用setInterval()方法执行 14: setInterval( "scroll()" , 500); 15: </script> 转载于:https: