settimeout

setTimeout not working inside forEach

爷,独闯天下 提交于 2019-12-31 08:57:25
问题 I have a forEach that calls a function. There needs to be a delay between each time it is called. I've put it inside a setTimeout inside the forEach. It isn't respecting the timeout after the first wait. Instead it is waiting once, then running all at once. I've set the timeout to 5 seconds and I am using a console to confirm. 5 seconds of wait, then several foobar console logs all at once. Why am I getting this behavior? var index = 0; json.objects.forEach(function(obj) { setTimeout(function

setTimeout doesn't run synchronously

廉价感情. 提交于 2019-12-31 04:39:06
问题 I'm trying to make a function to show some messages after 2 seconds in a 'for' function, but seems like isn't running in order at all, if I set one time higher, it will not wait until is done and just get to the next task. How to make setTimeout wait to finish before starting a new one? time += 2000; (function(set, cName, time) { if (cName == "A" || cName == "B") { setTimeout(function() { text.innerHTML = "somethibng <br />"+text.innerHTML; set.setAttribute("class", "type"+cName ); }, time);

Is setTimeout required?

北城余情 提交于 2019-12-31 03:14:28
问题 I have a question to async, await and setTimeout(). I thought, I use asynchron functions for slow processes. So I tried it with a large loop. On my Computer, it needs few seconds to run the following code: function slowFunction() { return new Promise(resolve => { setTimeout(() => { for (let i = 0; i < 4000000000; i++) {}; resolve('Ready at ' + new Date().toLocaleTimeString('de')); }, 0); }); }; console.log('Start: ' + new Date().toLocaleTimeString('de')); (async () => { console.log('Call slow

Recursive setTimeout in javascript

橙三吉。 提交于 2019-12-31 02:10:05
问题 Is it possible to run this "indefinitely", without causing stack overflow or running out of memory? function eternal(){ var time = recalculateTime(); doSomething(); setTimeout(eternal,time); } setTimeout(eternal,200000); I'm not using setInterval, because the triggering time is variable. 回答1: This is not actually a recursive call because the first invocation of eternal() has actually finished before the setTimeout() calls the next one. So, it's not technically recursion and does not build up

Issues with setTimeout() inside jQuery .each

房东的猫 提交于 2019-12-31 01:52:12
问题 The following code will not work properly. I've tried different variations & searching everywhere but no luck. i = 1; var timer = new Array(); jQuery('a').each(function($) { i++; timer[i] = setTimeout(jQuery(this).remove(), i * 5000) }) 回答1: Wrap remove element with a function i = 1; var timer = new Array(); jQuery('a').each(function($) { i++; var thiz = jQuery(this); timer[i] = setTimeout(function() { thiz.remove(); }, i * 5000); }) 回答2: The first parameter to setTimeout (or setInterval )

Why does Twitter redefine window.setTimeout and window.setInterval?

↘锁芯ラ 提交于 2019-12-30 04:49:06
问题 I was studying the Twitter source code, and I came across the following snippet: window.setTimeout=window.setTimeout;window.setInterval=window.setInterval; Why does Twitter redefine these functions? Edit: To see the code, go to any twitter user page, open the source of the page, and you will see that snippet in the second block of javascript. 回答1: This is a technique to replace setTimeout and setInterval functions globally in a cross-browser fashion. window.setTimeout , when used as an lvalue

ccc this 指针

被刻印的时光 ゝ 提交于 2019-12-30 03:29:02
cc.Class({ extends: cc.Component, properties: { musicPlayer: { default: null, type: cc.AudioSource }, dingClip: { default: null, url: cc.AudioClip }, cheeringClip: { default: null, url: cc.AudioClip } }, // use this for initialization onLoad: function () { var self = this; // play audioSource self.musicPlayer.play(); // play ding in 1 sec, play cheering in 2 sec setTimeout(function() { cc.audioEngine.playEffect(self.dingClip, false); setTimeout(function() { cc.audioEngine.playEffect(self.cheeringClip, false); }, 2000); }, 1000); }, // called every frame update: function (dt) { }, }); 来源: https

for循环中的setTimeout()

自作多情 提交于 2019-12-29 13:20:58
for(var i = 0;i<3;i++){ setTimeout(function(){ console.log(i) },1000) }; 打印结果:三个3 原因分析:setTimeout()是一个异步处理函数,它会等待所有的主线程任务处理完,才开始执行自己的内部的任务,每隔1s往任务队列中添加一个任务【闭包函数,setTimeout()中的函数,现在还没执行】,当主线程执行完时,这时i=3, 才开始执行任务队列中的任务【闭包函数,setTimeout()中的函数开始执行,执行三次】。 for循环是遵循js执行机制--从上到下,依次同步执行,for循环括号内的就是主线程,执行完时i是3,所以会打印出3次3; 如果想打印出0,1,2 解决方案: 把var改为let,let是块极作用域,每次for循环都会把对应的i绑定到添加的任务【闭包函数,setTimeout()中的函数】中,所以当主线程执行完时,也不会影响到每个任务中i。所以可以打印出0 1 2 把定时器放在一个自执行函数中用i当做参数 (function(i){ setTimeout(function(){ console.log(i); },1000) })(i) 来源: https://www.cnblogs.com/spencer66/p/11574236.html

Javascript setTimeout function repeat

假装没事ソ 提交于 2019-12-29 09:05:45
问题 Is there a way to repeat a function in Javascript using the setTimeout function? For example, I need two functions to be called every five seconds. I have something like this: $(document).ready(function(){ setTimeout('shiftLeft(); showProducts();', 5000); }); but it only happens once, five seconds after page loads, and I need it to happen every five seconds. 回答1: Use setInterval() instead of setTimeout() , if you want your function to be executed repeatedly. setTimeout() delays the execution

Call a Javascript function every x seconds and stop after y seconds?

荒凉一梦 提交于 2019-12-29 08:15:28
问题 I want to call a Javascript function after clicking a button, make it loop every 100 milliseconds for 1,5 seconds and then pause it. The idea is that I want to make this accordion slider able to show scrollable content using this custom jQuery scrollbar(I have no idea of any other better custom cross-browser scrollbar). The scrollbar must be reconstructed with this function every time the users clicks one of the items, using this: $(".ac-big").customScrollbar("resize") In order to make the