settimeout

jQuery/JavaScript: My recursive setTimeout function speeds up when tab becomes inactive

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-17 14:50:48
问题 I've got an odd little dilemma in this jQuery slideshow plugin that I am building. It's nothing fancy and the code I have written to date is working great however I have noticed that when I leave the site running and switch to a new tab and continue browsing the web in this other tab (Chrome for Mac in my case) that when I return to my site, the setTimeout call seems to have speed up and instead of waiting for the timer to finish the fire the event, it fires continuously. Here is my

Does calling setTimeout clear the callstack?

末鹿安然 提交于 2019-12-17 12:49:08
问题 Can a stack overflow be avoided in javascript by using the setTimeout method to call a function instead of calling it directly? My understanding of setTimeout is that it should start a new callstack. When i look in the callstack of both chrome and IE it seems that the setTimeout calls are waiting for the function call to return. Is this just a property of the debugger or is my understanding flawed? EDIT While the answers provided below are correct, the actual problem I was having was related

深入理解JS函数中this指针的指向

走远了吗. 提交于 2019-12-17 11:10:41
函数在执行时,会在函数体内部自动生成一个this指针。谁 直接调用 产生这个this指针的函数 ,this就指向谁。 怎么理解指向呢,我认为指向就是等于。例如直接在js中输入下面的等式: console.log(this===window);//true 情况不同,this指向的对象也不同。例如: 1. 函数声明的情况 var bj=10; function add(){ var bj=20; console.log(this);//window console.log(this.bj);//10 console.log(bj);//20 console.log(this.bj+bj);//30 } add(); window.add(); (1) 执行了add()之后,此时的this指向的是window对象,为什么呢?因为这时候add是全局函数,是通过window 直接调用 的。所以下面我专门写了个window.add()就是为了说明,全局函数的this都是指向的window。 (2) 就像alert()自带的警告弹窗一样,window.alert()执行之后也是一样的效果。所以只要是 window点 这种调用方式都可以省略掉,因此警告弹窗可以直接使用alert()。 2. 函数表达式 var bj=10; var zjj=function(){ var bj=30;

NodeJS Timeout a Promise if failed to complete in time

我们两清 提交于 2019-12-17 09:18:24
问题 How can I timeout a promise after certain amount of time? I know Q has a promise timeout, but I'm using native NodeJS promises and they don't have .timeout function. Am I missing one or its wrapped differently? Alternatively, Is the below implementation good in means of not sucking up memory, actually working as expected? Also can I make it somehow wrapped globally so I can use it for every promise I create, without having to repeat the setTimeout and clearTimeout code? function run() {

Get return value from setTimeout [duplicate]

淺唱寂寞╮ 提交于 2019-12-17 07:12:42
问题 This question already has answers here : How do I return the response from an asynchronous call? (36 answers) Closed 4 months ago . I just want to get the return value from setTimeout but what i get is a whole text format of the function? function x () { setTimeout(y = function () { return 'done'; }, 1000); return y; } console.log(x()); 回答1: You need to use Promises for this. They are available in ES6 but can be polyfilled quite easily: function x() { var promise = new Promise(function

Get return value from setTimeout [duplicate]

喜你入骨 提交于 2019-12-17 07:12:29
问题 This question already has answers here : How do I return the response from an asynchronous call? (36 answers) Closed 4 months ago . I just want to get the return value from setTimeout but what i get is a whole text format of the function? function x () { setTimeout(y = function () { return 'done'; }, 1000); return y; } console.log(x()); 回答1: You need to use Promises for this. They are available in ES6 but can be polyfilled quite easily: function x() { var promise = new Promise(function

.delay() and .setTimeout()

拟墨画扇 提交于 2019-12-17 06:09:52
问题 According to jQuery document on .delay() , The .delay() method is best for delaying between queued jQuery effects. Because it is limited—it doesn't, for example, offer a way to cancel the delay—.delay() is not a replacement for JavaScript's native setTimeout function, which may be more appropriate for certain use cases. Could someone please expand on this? When is it more appropriate to use .delay() , and when is it better to use .setTimeout() ? 回答1: I think what you posted explains itself

Maximum call stack size exceeded on SetTimeout recursive function (Javascript) [duplicate]

…衆ロ難τιáo~ 提交于 2019-12-17 05:15:18
问题 This question already has answers here : setTimeout calls function immediately instead of after delay (2 answers) Closed 3 years ago . I have a recursive SetTimeout function that clicks a filter on my page after the filters have loaded (they're loaded through Ajax, so not available immediately on page load). $scope.clickFilter = function () { var filter = $('.filter-item') .find('input[value="' + $scope.activeFilter + '"]'); if (filter.length < 1) { setTimeout($scope.clickFilter(), 1000); }

Javascript, setTimeout loops?

戏子无情 提交于 2019-12-17 03:36:27
问题 So I am working on a music program that requires multiple javascript elements to be in sync with another. I've been using setInterval which works really well initially however over time the elements gradually become out of sync which with a music program is bad. I've read online that setTimeout is more accurate, and you can have setTimeout loops somehow however I have not found a generic version that illustrates how this is possible. Could someone just show me a basic example of using

How to add pause between each iteration of jQuery .each()?

霸气de小男生 提交于 2019-12-17 02:46:07
问题 I'm grabbing an array of jQuery objects and then via .each() modifying each individual jquery with in the array. In this case I'm updated the class names to trigger a -webkit-transition-property to utilize a css transition. I'd like there to be a pause before each css transition begins. I'm using the following, but there is no delay in between each update. Instead, they all appear to be updating at once. function positionCards() { $cards = $('#gameboard .card'); $cards.each(function() {