settimeout

Javascript return value from setTimeout [duplicate]

馋奶兔 提交于 2019-12-24 16:55:15
问题 This question already has answers here : How do I return the response from an asynchronous call? (36 answers) Closed 3 years ago . I want do something like that: var x = function(){ if (controlVar === 0) { setTimeout(x, 300); } else { return value; }; Is there a method in js for call a function like in synchronous code (var z = x()) and return a value only when my controlVar turn in 1? This should not block, because when I use setTimeout the main loop shoul be able to take the control, but if

JS函数节流

早过忘川 提交于 2019-12-24 16:17:56
函数节流原文地址 :http://www.cnblogs.com/haoxl/archive/2016/03/07/5252409.html JS函数节流 背景: 在前端开发中,有时会为页面绑定resize事件,或为一个页面元素拖拽事件(其核心就是绑定mousemove)在一个正常操作中也有可能在一个短时间内触发非常多次事件绑定程序,而DOM操作是很消耗性能的,如果为这些事件绑定一些操作DOM节点的操作的话就会引发大量的计算,在用户看来页面可能就一时间没有响应,这个页面就变卡变慢了,甚至在IE下,如果绑定的resize事件进行较多DOM操作,其高频率可能直接就使得浏览器崩溃。 函数节流简单讲就是让一个函数无法在很短的时间间隔内连续调用,只有当上一次函数执行后过了你规定的时间间隔,才能进行下一次该函数的调用。 函数节流原理:用定时器,当触发一个事件时,先setTimout让这个事件延迟一会再执行,如果在这个时间间隔内又触发了事件,那我们就clear掉原来的定时器,再setTimeout一个新的定时器延迟一会执行。 优缺点: 《JavaScript高级程序设计》中介绍的函数节流 function throttle(method,context){ clearTimeout(method,tId); method.tId=setTimeout(function(){ method

【javascript】js中的函数节流和函数防抖

走远了吗. 提交于 2019-12-24 16:17:10
一、概念解释  函数节流和函数防抖,两者都是优化高频率执行js代码的一种手段。  大家大概都知道旧款电视机的工作原理,就是一行行得扫描出色彩到屏幕上,然后组成一张张图片。由于肉眼只能分辨出一定频率的变化,当高频率的扫描,人类是感觉不出来的。反而形成一种视觉效果,就是一张图。就像高速旋转的风扇,你看不到扇叶,只看到了一个圆一样。  同理,可以类推到js代码。在一定时间内,代码执行的次数不一定要非常多。达到一定频率就足够了。因为跑得越多,带来的效果也是一样。倒不如,把js代码的执行次数控制在合理的范围。既能节省浏览器CPU资源,又能让页面浏览更加顺畅,不会因为js的执行而发生卡顿。这就是函数节流和函数防抖要做的事。   函数节流 是指一定时间内js方法只跑一次。比如人的眨眼睛,就是一定时间内眨一次。这是函数节流最形象的解释。   函数防抖 是指频繁触发的情况下,只有足够的空闲时间,才执行代码一次。比如生活中的坐公交,就是一定时间内,如果有人陆续刷卡上车,司机就不会开车。只有别人没刷卡了,司机才开车。 二、函数节流  函数节流应用的实际场景,多数在监听页面元素滚动事件的时候会用到。因为滚动事件,是一个高频触发的事件。以下是监听页面元素滚动的示例代码: // 函数节流 var canRun = true; document.getElementById("throttle")

Why doesn't setTimeout wait to call a function?

試著忘記壹切 提交于 2019-12-24 13:25:01
问题 I want to create a simple game of sorts. I am trying to duplicate a div recursively after a few seconds. After duplicated, it creates the new div with a new unique ID (ID+i). The idea is that it keeps creating divs and the user has to click on them to remove them for as long as they can before it reaches the max (game over). It won't properly wait to create the divs. I want to create new divs from the existing one every few seconds, but it either creates all 15 as soon as I run it or it only

setTimeout only sees last value in array? [duplicate]

女生的网名这么多〃 提交于 2019-12-24 12:17:38
问题 This question already has answers here : Closed 8 years ago . Possible Duplicate: Javascript closure inside loops - simple practical example How do I pass the value (not the reference) of a JS variable to a function? Why always the last reference to the object is used in loop? I have an array of ids which I loop over and want to use in a function called by setTimeout, however when "func" below is executed it only seems to see the last id stored in the array. I have been trying to uses

javascript setTimeout() first argument: expression error

核能气质少年 提交于 2019-12-24 10:58:44
问题 function Timer() { this.initialTime = 0; this.timeStart = null; this.getTotalTime = function() { timeEnd = new Date(); diff = timeEnd.getTime() - this.timeStart.getTime(); return diff+this.initialTime; }; this.formatTime = function() { interval = new Date(this.getTotalTime()); return interval.getHours() + ":" + interval.getMinutes() + ":" + interval.getSeconds(); }; this.start = function() { this.timeStart = new Date(); setTimeout("this.updateTime()", 1000); }; this.updateTime = function() {

jQuery: Unable to fade out a previously faded in element

折月煮酒 提交于 2019-12-24 10:49:56
问题 I want to show a notification at the top – for 2 seconds – telling me which version of jQuery & jQuery UI was loaded. Unfortunately, I can't seem to be able to hide it later. My code $('<div>jQuery v' + jQuery.fn.jquery + ' and jQuery UI v' + jQuery.ui.version + ' loaded.</div>') .addClass('ui-state-highlight').prependTo('body').hide(0, function() { $(this).fadeIn(500, function() { setTimeout(function() { $(this).fadeOut(500, function() { $(this).remove(); }); }, 2000); }); }); jQuery Lint

SoundManager2 - Distortion When Stopping Sound

二次信任 提交于 2019-12-24 10:36:32
问题 I am trying to loop through a bunch of sound files in SoundManager2 and play them at various points in time. The code below takes a songArray that looks like this, with the "note" consisting of a sound file and a duration for how long to play that sound file: var songArray = [[null],[["clarinet_e4.mp3",1],["clarinet_a4.mp3",1]],[null],[null],[null],[null],[["clarinet_c4.mp3",1]],[["clarinet_c5.mp3",1]],[null],[["clarinet_ab4.mp3",1]],[["clarinet_f3.mp3",1]],[null],[["clarinet_g4.mp3",1]],[[

Nested Promise with setTimeout

ぐ巨炮叔叔 提交于 2019-12-24 10:10:06
问题 I have a nested promise. The promise resolves or rejects based on another promise resolving or rejecting with a setTimeout of 0 so as not to clog: return new Promise((resolve, reject) => { promiseInst .then(value => { executeSoon(() => { dispatch({ ...action, status: "done", value: value }); resolve(value); }); }) .catch(error => { executeSoon(() => { dispatch({ ...action, status: "error", error: error.message || error }); reject(error); }); }); }); the executeSoon() is executeSoon(fn) {

How to pause a setTimeout function

这一生的挚爱 提交于 2019-12-24 07:18:40
问题 I have this code to start a setTimeout function with a button and to stop it with another button, but if I stop it and start it, it will start over, Is there any way to get a pause/continue button? Javascript code: var timeout1, timeout2, timeout3; function timedText() { timeout1 = setTimeout(desertAK, 1000) timeout2 = setTimeout(fourAlarmShotgun, 5000) timeout3 = setTimeout(frostbiteAR, 9000) } function stopTimeout() { clearTimeout(timeout1); clearTimeout(timeout2); clearTimeout(timeout3); }