setinterval

JavaScript 关于setTimeout与setInterval的小研究

时光怂恿深爱的人放手 提交于 2019-12-05 19:33:40
说明 在开发功能“轨迹播放”时,遇到了一个情况。 原先同事已经开发了一版,这次有个新功能:点击线上任意一点后可以从点击处重新播放。 看了一下原来的版本,发现同时使用了setTimeout和setInterval,两者配合实现点线播放。 简单结构如下 function test() { setInterval(function () { console.log("interval"); //省略插值方法得到arr (...) play(arr); }, 2000); } function play(arr) { setTimeout(function () { play(arr); console.log("setTimeout"); }, 40); } 我觉得这个结构欠妥,两个定时器配合必定会出现失误!因此重构了一版,将两个定时器改为一个,用setInterval解决。 但是此时我并不知道欠妥欠在什么地方,缺乏理论支持,现在闲下来仔细研究了一下 找问题 在仔细研究了旧版本后,我先把旧版本结构扒了出来,排除其他因素,自己模拟了一个简单版(就是上面的代码) setTimeout:在执行时,是在载入后延迟指定时间后,去执行一次表达式,仅执行一次 setInterval:在执行时,它从载入后,每隔指定的时间就执行一次表达式 实验一:在使用setInterval和setTimeout方法上

how to unit-test setInterval in karma angularjs

99封情书 提交于 2019-12-05 18:24:03
app.directive('shuffleBlocks', function($timeout){ return { link: function(sco,ele,att){ if (itemCnt <= 1) return; /*Trigger function*/ function triggerEvent(){ ... } ele.bind('click', triggerEvent); setInterval(triggerEvent, 5000); } } }) here I wrote the test var elem = '<div shuffle-blocks><div>'; elem = mockCompile(elem)(rootScope.$new()); setInterval(function(){ expect(......).toBe(....) }); Obviously this is not the right method, does anyone know how to test $timeout and setInterval in karma? tennisgent UPDATE: The proper method of mocking setInterval in an angular 1.2+ application is to

jquery addClass and removeClass with setInterval

守給你的承諾、 提交于 2019-12-05 14:23:13
I want to change class name every 3 seconds. Bu it doesn't work. How can I do this? $(document).ready(function() { function moveClass(){ var x = $('.liveEvents'); x.removeClass('liveEvents'); x.addClass('liveEventsActive'); x.removeClass('liveEventsActive'); x.addClass('liveEvents'); } setInterval(moveClass, 3000); return false; }); You can do this in one line. Use toggleClass : setInterval(function(){$('.liveEvents').toggleClass('liveEventsActive')}, 3000); If you do your CSS correctly, you don't need to remove the liveEvents class. Just make the liveEventsActive class overwrite what you need

javascript + jquery + setinterval + animation

旧城冷巷雨未停 提交于 2019-12-05 14:06:47
I'm having a problem with setInterval and jquery animate. Here is my code: function slides1() { ... $("table#agah1").animate({ "left": first1 }, "slow"); $("table#agah2").animate({ "left": first2 }, "slow"); } $(function () { cyc = setInterval("slides1()", 3000); }); When switch to another browser tab, and return after a time, the animation keep doing it without delay, for the time I've been away from the tab, and then act correct. I've added these also without any luck: $(window).focus(function () { jQuery.fx.off = false; cyc = setInterval("slides1()", 3000); }); $(window).blur(function () {

Angular2 refreshing view on array push

限于喜欢 提交于 2019-12-05 13:37:03
问题 I can't seem to get angular2 view to be updated on an array.push function, called upon from a setInterval async operation. the code is from this angular plunkr example of setInterval: What i'm trying to do is as follows: import {View, Component, bootstrap, Directive, ChangeDetectionStrategy, ChangeDetectorRef} from 'angular2/angular2' @Component({selector: 'cmp', changeDetection: ChangeDetectionStrategy.OnPush}) @View({template: `Number of ticks: {{numberOfTicks}}`}) class Cmp { numberOfTicks

Wait until setInterval() is done

ⅰ亾dé卋堺 提交于 2019-12-05 12:05:51
问题 I would like to add a small dice rolling effect to my Javascript code. I think a good ways is to use the setInterval() method. My idea was following code (just for testing): function roleDice() { var i = Math.floor((Math.random() * 25) + 5); var j = i; var test = setInterval(function(){ i--; document.getElementById("dice").src = "./images/dice/dice" + Math.floor((Math.random() * 6) + 1) + ".png"; if(i < 1) { clearInterval(test); } }, 50); } Now I would like to wait for the setInterval until

Clear all setIntervals

孤人 提交于 2019-12-05 11:20:52
I'm using setIntervals within an each() function like so $(".elements").each(function() { setInterval(function() { }, 1000); }); Obviously a setIntervals is created for each element. My question is: How do I clear all the setIntervals when I no longer need them? I have tried storing the setInterval in a variable and call window.clearInterval(int) , but that only clears the last setInterval since each variable is overridden. When you set an interval, you get a pointer to it: var myInterval = setInterval(function(){}, 4000); If you want to cancel an interval, you do the following: clearInterval

Adding pause on hover to setInterval()?

谁都会走 提交于 2019-12-05 08:08:17
Hi here's my jquery function for slideshow with other inbuilt function. I have been trying to make it pause when i hover over the overall div. Help me out! $(function() { var timer = setInterval( showDiv, 5000); var counter = 2; function showDiv() { if (counter ==0) { counter++; return; } $('div','#slide-show-overall') .stop() .fadeOut('slow') .filter( function() { return this.id.match('picture-set-' + counter); }) .fadeIn('slow'); if (counter ==2) { slideWings();} //extra functions if (counter ==1) { resetWings();} //extra functions counter == 3? counter = 1 : counter++; } }); }); <div style=

javascript中的setInterval()和setTimeout()用法比较

我怕爱的太早我们不能终老 提交于 2019-12-05 05:38:37
setTimeout()和setInterval() 何时被调用执行 定义 setTimeout()和setInterval()经常被用来处理延时和定时任务。 setTimeout () 方法用于在 指定的毫秒数后 调用函数或计算表达式,而 setInterval ()则可以在 每隔指定的毫秒数 循环调用函数或表达式,直到clearInterval把它清除。 从定义上我们可以看到两个函数十分类似,只不过前者执行一次,而后者可以执行多次,两个函数的参数也相同,第一个参数是要执行的code或句柄,第二个是延迟的毫秒数。 很简单的定义,使用起来也很简单,但有时候我们的代码并不是按照我们的想象精确时间被调用的,很让人困惑 简单示例 看个简单的例子,简单页面在加载完两秒后,写下Delayed alert! setTimeout('document.write("Delayed alert!");', 2000); 看起来很合理,我们再看个setInterVal()方法的例子 按 Ctrl+C 复制代码 按 Ctrl+C 复制代码 页面每隔1秒记录一次当前时间(分钟:秒:毫秒),记录十次后清除,不再记录。考虑到代码执行时间可能记录的不是执行时间,但时间间隔应该是一样的,看看结果 43:38:116 43:39:130 43:40:144 43:41:158 43:42:172 43:43

Adding a Quiz Timer, Fade Out/Skip to the next if timer reaches 0

别说谁变了你拦得住时间么 提交于 2019-12-05 04:46:20
I'm really new to jQuery but familiar with some other languages. I recently bought a quiz type script and I'm trying to add a simple 15 second timer to each question. It's only a fun quiz, so no need to worry about users playing with the javascript to increase time etc. Basically, if a user does not pick a question within 15 seconds, it will automatically go on to the next question and the timer starts over again. Answers have the .next tag, and when chosen it moves onto the next question as the code below shows (hopefully). superContainer.find('.next').click(function () { $(this).parents('