settimeout

JavaScript 中的 this 详解

谁说胖子不能爱 提交于 2019-12-12 20:17:14
【推荐】2019 Java 开发者跳槽指南.pdf(吐血整理) >>> 首先,我知道这篇文章很无聊,无非就是关于 js 中的 this,并且也已经有千千万万的文章写过这部分内容了; 但是,我还是想写一篇关于 js 中的 this 的文章,算是一个总结归纳吧;(大神们可以绕行) 在 js 中,this 这个上下文总是变化莫测,很多时候出现 bug 总是一头雾水,其实,只要分清楚不同的情况下如何执行就 ok 了。 全局执行 首先,我们在全局环境中看看它的 this 是什么: first. 浏览器: console.log( this ); // Window {speechSynthesis: SpeechSynthesis, caches: CacheStorage, localStorage: Storage, sessionStorage: Storage, webkitStorageInfo: DeprecatedStorageInfo…} second. node: console.log( this ); // global 可以看到打印出了 global 对象; 总结 :在全局作用域中它的 this 执行当前的全局对象(浏览器端是 Window ,node 中是 global )。 函数中执行 纯粹的函数调用 这是最普通的函数使用方法了: function test()

jQuery Ajax setTimeout JSON

限于喜欢 提交于 2019-12-12 18:06:43
问题 I am using the jQuery AJAX to call JSON service. I am then spitting out the data to .html. I would like 2 things to happen. 1. I want a refresh button to refresh the data, not the whole page. 2. I want a setTimeout or setInterval (which ever works best) to update the data every 5 minutes or so. But refresh the page. How would I wrap the AJAX in a setTimeout or setInterval or refresh the data using a button and a timer every 5 minutes or so. I know this should be simple but I have not been

understanding execution of setTimeout() functions that follow one another

家住魔仙堡 提交于 2019-12-12 17:50:57
问题 I need to execute multiple functions one after another in fixed time intervals, thus using setTimeout. I want to ensure that I understand how it executes. I have following logic: setTimeout(function() { //Execute first function }, 200); setTimeout(function() { //Execute second function }, 400); setTimeout(function() { //Execute third function }, 600); Does this mean that first function executes after 200ms, second one 200ms after the first, and the third one 200ms after the second and so on?

Waiting in JavaScript beyond wrapping everything in setTimeout

风流意气都作罢 提交于 2019-12-12 17:33:01
问题 So, I'm creating a bot to play a video game online (completely legally, there's a server for bot competitions, don't worry). The code is in JavaScript, which I'm just picking up now, and I'm having trouble telling the bot to do something, then wait either by time or by condition until doing something else. So far, I'm simply wrapping everything in gigantic setTimeout commands, but this is extremely inelegant. There's not too much code that's relevant to post, but essentially I've made up a

recursive setTimeout() pause on background

流过昼夜 提交于 2019-12-12 16:00:50
问题 I have a piece of code: var logo = $("#blinking-logo"); function logo_blink() { logo.fadeOut(10).delay(10) .fadeIn(10).delay(20) .fadeOut(10).delay(10) .fadeIn(10) window.setTimeout(logo_blink, (Math.random()*(1500))+1500); } logo_blink(); All it makes is blinking a picture once in ~30 seconds (time is less here for easier debugging) The problem that Chrome pauses this timer while the tab in backgrounded, and then, when coming back to that tab, it blinks all the blinks that were missed in

iframe loading time limit use javascript

旧时模样 提交于 2019-12-12 09:28:06
问题 i need to stop loading my iframe page after 5000 ms i'm use these but it's refresh the iframe every 5000 ms later what the problem . pleas fix it pleas. thanks <iframe id="iframe1" src="" width="920" height="900" border="2"></iframe> <script type="text/javascript"> function setIframeSrc() { var s = "http://lx5.in/CGIT-Results-p2-2013.php"; var iframe1 = document.getElementById('iframe1'); if ( -1 == navigator.userAgent.indexOf("MSIE") ) { iframe1.src = s; setTimeout(setIframeSrc, 5000); }

HTML5 audio object doesn't play on iPad (when called from a setTimeout)

萝らか妹 提交于 2019-12-12 08:08:49
问题 I have a page with a hidden <audio> object which is being started and stopped using a custom button via javascript. (The reason being I want to customise the button, and that drawing an audio player seems to destroy rendering performance on iPad anyway). A simplified example (in coffeescript): // Works fine on all browsers constructor: (@_button, @_audio) -> @_button.on 'click', @_play // Bind button's click event with jQuery _play: (e) => @_audio[0].play() // Call play() on audio element The

why does javascript setTimeout() not work in a loop? [duplicate]

霸气de小男生 提交于 2019-12-12 08:03:52
问题 This question already has answers here : JavaScript closure inside loops – simple practical example (44 answers) Closed 6 years ago . Consider the following code: <!DOCTYPE html> <html> <head> <script> function timedText() { var x=document.getElementById('txt'); var t = new Array(); t[1] = setTimeout( function(){x.value="2 seconds"}, 2000 ); t[2] = setTimeout( function(){x.value="4 seconds"}, 4000 ); t[3] = setTimeout( function(){x.value="6 seconds"}, 6000 ); } function timedTextArr() { var x

how do I setInterval to call a function within a class

故事扮演 提交于 2019-12-12 07:18:51
问题 I have a class like: function run(){ this.interval; this.start = function(){ this.interval = setInterval('this.draw()',1000); }; this.draw = function(){ //some code };} var run = new run(); run.start(); however I can't seem to reference/call this.draw() within the setInterval, it says this.draw() is not a function, and if I remove the quotes it says useless setInterval call, what am I doing wrong? 回答1: The value of this is set depending on how a function is called. When you call a function as

setTimeout function not working when looping through markers on Gmaps API

浪尽此生 提交于 2019-12-12 06:13:33
问题 I am trying to draw 50 markers in a row with 2 seconds in between each marker. The first marker gets drawn in GPS position (x, y) and then two seconds later, the second marker gets drawn in GPS position (x + 0.0004, y) and continues until the 50th marker is created on the map. I have placed the marker constructor inside a setTimeout function. The setTimeout function is placed inside a for loop. However, when I run this, the loop runs through the 50 markers instantaneously (ie without the 2