settimeout

setTimeout 的黑魔法

邮差的信 提交于 2019-12-21 23:52:55
setTimeout,前端工程师必定会打交道的一个函数.它看上去非常的简单,朴实.有着一个很不平凡的名字--定时器.让年少的我天真的以为自己可以操纵未来.却不知朴实之中隐含着惊天大密.我还记得我第一次用这个函数的时候,我天真的以为它就是js实现多线程的工具.当时用它实现了一个坦克大战的小游戏,玩儿不亦乐乎.可是随着在前端这条路上越走越远,对它理解开始产生了变化.它似乎开始蒙上了面纱,时常有一些奇怪的表现让我捉摸不透.终于,我的耐心耗尽,下定决心,要撕开它的面具,一探究竟. 要说setTimeout的渊源,就得从它的官方定义说起.w3c是这么定义的 setTimeout() 方法用于在指定的毫秒数后调用函数或计算表达式。 看到这样一个说明,我们明白了它就是一个定时器,我们设定的函数就是一个"闹钟",时间到了它就会去执行.然而聪明的你不禁有这样一个疑问,如果是settimeout(fn,0)呢?按照定义的说明, 它是否会立马执行 ?实践是检验真理的唯一标准,让我们来看看下面的实验 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title></title> </head> <body> <script> alert(1); setTimeout("alert(2)", 0); alert(3); <

Fade out jQuery menu after delay

匆匆过客 提交于 2019-12-21 21:43:04
问题 I'm working on a jQuery drop-down menu that fades in when you hover on the top-level items. I want to set it so that when you move the mouse away the menu doesn't disappear instantly. I have this code: $(document).ready(function(){ $('ul#menu > li').hover( // mouseover function(){ $(this).find('>ul').fadeIn('fast'); }, // mouseout function(){ setTimeout( function(){ alert('fadeout'); $(this).find('>ul').fadeOut('fast') }, 1000 ); } ); }); After a second the alert happens, but the menu isn't

javascript - setTimeout() vs setInterval()

南笙酒味 提交于 2019-12-21 21:39:19
问题 What are the minor and major differences between setTimeout() and setInterval() ? I searched the internet but it made me confused! What's the difference between those? 回答1: The main diffrence is setInterval fires again and again in intervals, while setTimeout only fires once. you can get more differnces in simple words in setTimeout or setInterval? 'setInterval' vs 'setTimeout' 回答2: From Javascript timers MDN setTimeout () Calls a function or executes a code snippet after specified delay.

How to pass intervalID to interval function in javascript?

谁说胖子不能爱 提交于 2019-12-21 20:26:37
问题 In javascript when I create an interval, I want to stop the interval from inside the function, but I don't want to reference the ID value from outside like this var y = setInterval(function(){ clearInterval(y); }, 1000); What I want is to pass a variable similar to this style setTimeout(function(data){alert(data);}, 1000, "data"); This works for setInterval too, except I can't really pass the id value that's returned by the setInterval function, because it gets created after calling it. Right

Javascript Timed Notifications - setTimeout, setInterval

ⅰ亾dé卋堺 提交于 2019-12-21 20:18:19
问题 I am creating a web app that allows users to manage a calendar (CRUD events, tasks, reminders etc...) And I am trying to implement a feature where they will receive a popup reminder x-minutes before the event/task. From my understanding there is really only one way to do this with javascript: On login, check for any upcoming events in the database (say in the next 12 hours) and create a setTimeout for the next event, when that setTimeout executes, check again for next event and so on... My

Why does setTimeout(location.reload) throw a TypeError? [duplicate]

我是研究僧i 提交于 2019-12-21 19:27:10
问题 This question already has answers here : Why can't I pass “window.location.reload” as an argument to setTimeout? (3 answers) Closed 3 years ago . I'm trying to understand the strange behavior of this code: window.setTimeout(window.location.reload, 200); In Firefox this throws a TypeError: TypeError: 'reload' called on an object that does not implement interface Location. In Chromium this throws another TypeError: Uncaught TypeError: Illegal invocation These two alternatives work fine: window

Bypassing IE's long-running script warning using setTimeout

怎甘沉沦 提交于 2019-12-21 18:56:25
问题 I've asked about this before, and have found a few articles online regarding this subject, but for the life of me I cannot figure this out. I have a set of Javascript functions that calculate a model, but there's a ton of looping going on that makes the script take a while (~4 seconds). I don't mind the processing time, but IE prompts with a warning since there are so many executions. I've tried optimizing my code, but I simply can't cut down the number of executions enough to bypass the IE

Way to execute jQuery member functions inside setTimeout without closure?

谁说我不能喝 提交于 2019-12-21 14:51:30
问题 I was trying to do something along these lines: setTimeout($('#element').hide,3000); which seems simple enough, but it is crippled by the "this" problem. I want to find a way to just pass the actual function as a parameter, without wrapping it in another function , e.g. I do not want to do this: setTimeout(function(){$('#element').hide();},3000); What I've tried: setTimeout($('#element').hide,3000); setTimeout($('#element').hide.apply(document),3000); /* jQuery docs say that document is the

Promise.resolve with no argument passed in

 ̄綄美尐妖づ 提交于 2019-12-21 09:19:23
问题 In the OpenUI5 code-base I came across this snippet: // Wait until everything is rendered (parent height!) before reading/updating sizes. // Use a promise to make sure // to be executed before timeouts may be executed. Promise.resolve().then(this._updateTableSizes.bind(this, true)); It looks like the native Promise function is being used, with no argument being passed to it's resolve function which takes an: Argument to be resolved by this Promise. Can also be a Promise or a thenable to

setTimeout() - in for loop with random delay [duplicate]

时间秒杀一切 提交于 2019-12-21 07:50:42
问题 This question already has answers here : Closed 7 years ago . Possible Duplicate: Javascript closure inside loops - simple practical example Seen many posts talking about setTimeout and closures but I'm still not able to pass in a simple for loop counter. for (i = 0; i < 5; i++) { setTimeout(function () { console.log(i); }, Math.floor(Math.random() * 1000)); } Gives 5 5 5 5 5 Would like to have 0 1 2 3 4 What's wrong ? Please don't flame, I thought I have understood the setTimeout() tale but