settimeout

解决var在setTimeout中的办法

回眸只為那壹抹淺笑 提交于 2019-12-10 21:16:02
解决作用域的问题 for (var i = 1; i <= 5; i++) { setTimeout(function timer() { console.log(i) }, i * 1000) } 立即执行 for (var i = 1; i <= 5; i++) { ;(function(j) { setTimeout(function timer() { console.log(j) }, j * 1000) })(i) } 使用setTimeout的第三个参数, 定时器启动时候,第三个以后的参数是作为第一个 func() 的参数传进去 for (var i = 1; i <= 5; i++) { setTimeout( function timer(j) { console.log(j) }, i * 1000, i ) } 第三种使用let for (let i = 1; i <= 5; i++) { setTimeout(function timer() { console.log(i) }, i * 1000) } 来源: https://www.cnblogs.com/lipu12281/p/12019129.html

In node.js if no response is received from an http request, how do you know?

独自空忆成欢 提交于 2019-12-10 18:54:12
问题 OK so in the below example I request something from a server. If a response comes back I parse the JSON and add the data to my mongodb. However if NO response comes back, then no event fires evidently. How would I add a timeout to this so that if no response is received, then I can cancel the request without any errors being thrown, and call a function? (I'm going to make it call a function that emails me.) Thanks! var req = https.request(options, function(res) { res.on('data', function(d) {

Jasmine: test a setTimeout function throws an error

こ雲淡風輕ζ 提交于 2019-12-10 17:26:50
问题 I would like to test the error handling of a method, which schedules work with setTimeout. The error will be thrown in the scheduled part, i.e.: function sutWithSetTimeout() { setTimeout(function () { throw new Error("pang"); }, 1); } How do I test that an error is thrown and that it has the correct message? 回答1: You need to catch the function in the setTimeout and call them using expect(function(){fn();}).toThrow(e); . So you can spy on setTimeout to get the function: spyOn(window,

Calling setTimeout in a loop not working as expected

自作多情 提交于 2019-12-10 17:17:29
问题 The following JavaScript ought to (in my mind) play a sequence of notes 0.5 sec apart. But it plays them all as a single simultaneous chord. Any idea how to fix it? function playRecording() { if (notes.length > 0) { for (var i = 0; i < notes.length; i++) { var timeToStartNote = 500 * i; setTimeout(playNote(i), timeToStartNote); } } } function playNote(i) { var noteNumber = notes[i]; var note = new Audio("/notes/note_" + noteNumber + ".mp3"); note.play(); } 回答1: Very simple actually, in the

How to pause a function in JavaScript? [duplicate]

感情迁移 提交于 2019-12-10 16:52:16
问题 This question already has answers here : What is the JavaScript version of sleep()? (75 answers) Closed 5 years ago . I have a long function in JavaScript and I am going to pause it at some points. I know that I can do that using setTimeout(myFunction, 100) and clearTimeout() . But, as I know, after using setTimeout(myFunction, 100) my function would be executed from its first line after 100 milliseconds and thereafter every 100 milliseconds. I am wondering if I can pause my function on its

Complete all functions inside a FOR LOOP before iterating to next iteration of the loop

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-10 15:27:49
问题 Say i have a function like : var bigArray = [1,2,3,4,5.........n]; for(var i=0; i<bigArray.length; i++){ if(someCondition){ setTimeout(function(){ ..do_stuff.. callSomeFunction(); }, someDelayTime); } if(someCondition){ setTimeout(function(){ ..do_stuff.. callSomeFunction(); }, someDelayTime); } if(someCondition){ setTimeout(function(){ ..do_stuff.. callSomeFunction(); }, someDelayTime); } . . . . . . . . if(someCondition){ setTimeout(function(){ ..do_stuff.. callSomeFunction(); },

Prevent onbeforeunload function from pausing javascript timer

我的梦境 提交于 2019-12-10 15:16:22
问题 In my webpage, I have a countdown timer using javascript's setTimeout() . function Tick() { if (RemainingSeconds <= 0) { alert("Time is up."); return; } RemainingSeconds -= 1; ElapsedSeconds += 1; UpdateTimerDisplay(); window.setTimeout("Tick()", 1000); } I also have a function triggered on onbeforeunload to "prevent" the user from leaving the page. window.onbeforeunload = function () { if (!isIEAjaxRequest) { return "You should use the logout button to leave this page!"; } else {

前端BOM对象

孤人 提交于 2019-12-10 13:21:59
location.href 查看当前的url location.href http://www.baidu.com 跳转URL location.reload 重载当前页面 windows.alert('你好') 弹出提示框 document.body.coneditable=true 可对网页进行编辑 confirm("你吃饭了吗?") 弹出选择框 prompt("提示框") 计时器:   setTimeout("alert(123);",5) | setTimeout("js语句;",毫秒) 来源: https://www.cnblogs.com/cou1d/p/12016015.html

how to cleartimeout in node.js

给你一囗甜甜゛ 提交于 2019-12-10 12:44:39
问题 Hi We are developing application in node.js , socket.io , and redis. we have this procedure : exports.processRequest = function (request,result) { var self = this; var timerknock; switch(request._command) { case 'some command': // user login with username // some statement timerknock=setTimeout(function() { //some statemetn },20*1000); case 'other command ': // some statement clearTimeout(timerknock); } }; but when it cancel the timer it is not getting canceled when other command is executed

jquery setTimeout inside each

我的未来我决定 提交于 2019-12-10 12:19:08
问题 i have some code similar to this, that moves inside some images... it works but it doesn't seem to respect timer var i = 1; var indexArray = array(1,2,3); var timerx = new Array(); $( indexArray ).each(function( indexArraykey ) { function internalCallback ( i, indexArraykey ) { val = indexArray[indexArraykey]; console.log("test " + i + val); }); timerx[i] = setTimeout( internalCallback( i, indexArraykey ), i * 500000 ); i++; }); 回答1: A few points : i has the value of end of loop by the time