settimeout

setInterval By the minute On the minute

霸气de小男生 提交于 2019-12-21 07:11:09
问题 To the javascript enthusiasts, how would you program a setTimeOut (or setInterval ) handle to fire by the minute on the minute. So for example, if it is the 51 second of the current time, then fire it in 9 seconds, if it is the 14th second then fire it in 46 seconds thanks 回答1: var date = new Date(); setTimeout(function() { setInterval(myFunction, 60000); myFunction(); }, (60 - date.getSeconds()) * 1000); Obviously this isn't 100% precise, but for the majority of cases it should be sufficient

jQuery show setTimeout timer

半城伤御伤魂 提交于 2019-12-21 04:52:26
问题 I'm trying to build a simple countdown application. Is it possible to show the timer value on setTimeout, or would I have to use a for loop? Thanks! 回答1: with setTimeout : var n = 100; setTimeout(countDown,1000); function countDown(){ n--; if(n > 0){ setTimeout(countDown,1000); } console.log(n); } or using setInterval : var n = 100; var tm = setInterval(countDown,1000); function countDown(){ n--; if(n == 0){ clearInterval(tm); } console.log(n); } 回答2: <script> var timer = setInterval("mytimer

setTimeout not working in Greasemonkey user script when JS disabled in browser

我是研究僧i 提交于 2019-12-21 04:46:08
问题 I'm working on a project that requires my user script be run on pages as they are rendered without executing any of the page's JavaScript. That is to say, we need to browse with JavaScript disabled. I've encountered a problem though when I try to delay execution of a function within my script. Whenever I make a call to window.setTimeout , the function I pass in never gets executed. I think maybe this function is actually getting called on unsafeWindow instead of window. Is there any

Storing the return value of node.js setTimeout in redis

余生长醉 提交于 2019-12-21 03:38:20
问题 I'm using setTimeout in Node.js and it seems to behave differently from client-side setTimeout in that it returns an object instead of a number. I want to store this in redis, but since redis only stores strings, I need to convert the object to a string. However, using JSON.stringify throws a circular reference error. How can I store this object in redis if I want to be able to fetch it from redis and call clearTimeout on it? 回答1: You cannot store the object in Redis. The setTimeout method

how to wait with setTimeout until a variable get loaded and, at the same time, receive HTTP requests!

可紊 提交于 2019-12-21 02:28:12
问题 I' ve made a in JavaScript function to check every 100 ms if a global variable is loaded. When the variable will be loaded the function will return the value of the variable as shown below. In my code I use an HTTP server in JavaScript, and the variable will be loaded when a specific HTTP request with specific headers arrive to my server. function checkVariable() { if ( myvar != null ) { return myVar; } else { window.setTimeout("checkVariable();",100); } } I use this function in a piece of

循环异步事件

微笑、不失礼 提交于 2019-12-21 01:03:08
for(var i=0 ;i<5; i++){ setTimeout(function(){ console.log(i) },0) } //5,5,5,5,5 此处i是全局作用域 每次循环 新的i都会冲刷掉旧的i 好比皇位更替 for(let i=0 ;i<5; i++){ setTimeout(function(){ console.log(i) },0) } //0,1,2,3,4 此处i是函数作用域 每次循环 新的i都给自己开辟一块属于只自己的作用域 好比各个封地的诸侯 来源: 51CTO 作者: 喝醉的熊 链接: https://blog.51cto.com/13550695/2460527

Is clearTimeout necessary after setTimeout with 0ms?

怎甘沉沦 提交于 2019-12-20 23:24:09
问题 As i have already learned (here: https://www.youtube.com/watch?v=8aGhZQkoFbQ) it can be useful in some cases to call a setTimeout with 0ms delay (because of the event loop). Now ususally whenever I use setTimeout I also take care to call clearTimeout at the appropriate spot to make sure nothing remains somewhere and gets executed at a point where I do not want it to be executed. So my question is: Is it necessary (does it make sense) to call clearTimeout after a setTimeout with 0ms? The

研究javascript中的this

此生再无相见时 提交于 2019-12-20 20:22:33
在js中,this是根据它的作用域来指向它的上下文环境。 全局执行 在全局环境中,我们看看它的this是什么: 可以看到返回的是一个window对象。 在node中,this返回的是global对象。 总结 :在全局作用域中它的 this 执行当前的全局对象(浏览器端是 Window ,node 中是 global )。 函数中执行 纯粹的函数调用 我们可以看到,一个函数被直接调用的时候,属于全局调用,这时候它的this指向全局对象。 严格模式 ‘use strict’; 如果在严格模式的情况下执行纯粹的函数调用,那么这里的的 this 并不会指向全局,而是 undefined ,这样的做法是为了消除 js 中一些不严谨的行为: 在控制台中,打印出undefined. 当然,把它放在一个立即执行函数中会更好,避免污染全局: 作为对象的方法调用 当一个函数被当做对象的方法调用的时候: 这时候,this指向了当前这个对象, 当然,我们还可以这么做, 同样不变,因为js中一切皆对象,函数也是一个对象,对于test,它只是一个函数名,函数的引用,它指向这个函数,当 foo=test时,foo同样也指向了这个函数。 如果把对象的方法赋值给一个变量,然后直接调用这个变量呢: 可以看到,这时候 this 执行了全局,当我们把 test = obj.foo , test 直接指向了一个函数的引用

HTML5 video autoplay but with a 5 seconds of delay

荒凉一梦 提交于 2019-12-20 18:37:33
问题 I have a 20 second long HTML5 video loop as the background on my webpage and it is set to autostart. Is it possible to delay the video autoplay for 5 seconds? I am trying to allow the video to load completely before trying to play to prevent it from stuttering as much. Here is my current code: <video id="video_background" poster="images/dmm_background.jpg" controls="controls" preload="true" autoplay="true" loop="loop" muted="muted" volume="0"> <source src="videos/backgroundvideo.mp4" type=

jQuery: Fire mousemove events less often

混江龙づ霸主 提交于 2019-12-20 15:23:54
问题 I'm trying to figure out a clean way to aggregate mousemove events so that I ensure my code gets called, but only once every 250-300 milliseconds. I've thought about using something like the following, but was wondering if there was a better pattern, or something jQuery provides that will do the same thing: var mousemove_timeout = null; $('body').mousemove(function() { if (mousemove_timeout == null) { mousemove_timeout = window.setTimeout(myFunction, 250); } }); function myFunction() { /* *