标签: settimeout

使用 setTimeout 的变量范围

我很高兴它可以工作,但仍然对以下代码中“me”变量的范围有些困惑。现在使用它一段时间,但无法弄清楚它为什么起作用。

var timer=function(){
    this.timerMember=1;
    this.timerID=0;
    this.startTimer=function(){
        var me=this;
        this.timerID=setTimeout(function(){
            //shares scope with this.startTimer
            //timerMember is 2 here
            console.log(me.timerMember);
            // this is window
            console.log(this);
            // me doesn't exist in window
            console.log(this.me);
        },0);
//  this code gets executed before anonymous
//  timer function
//        clearTimeout(this.timerID);
        this.timerMember++;
    }
}
var t=new timer();
t.startTimer();
Run Code Online (Sandbox Code Playgroud)

传递给 setTimeout 的匿名函数似乎与 timer.startTimer 共享范围,但当匿名函数执行时 startTimer 显然已完成(me.timerMemer=2),因此当 startTimer 完成时,me 变量应该超出范围。幸运的是,JavaScript 会一直保留它,直到执行匿名函数(适用于所有浏览器),但我想知道这是否是正确的方法。这种行为是有意为之还是只是一个幸运的意外?

javascript scope settimeout

1
推荐指数
1
解决办法
2027
查看次数

setTimeout 和clearTimeout 分配给 var 并指定

var nothover = function(){window.setTimeout(function() {
     $('#proFBfeel').hide();
      $('#proFBfeel .moreinfos').html('');
       $('.contact_pro').html('');
     },3000);
   };
  $('#proFBfeel').nothover();
    $('#proFBfeel').hover(function() {
        window.clearTimeOut(nothover);
     });
         $('html').click(function() {
           $('#proFBfeel').hide();
            $('#proFBfeel .moreinfos').html('');
             $('.contact_pro').html('');
          });
Run Code Online (Sandbox Code Playgroud)

好的,正如您在我分配的内容中看到的,varnothover是 setTimeout

三秒后,我希望该函数运行,除非该对象悬停。如果悬停则清除超时。

然后,一旦回到对象外部,再次运行该函数,或者除非他们单击 HTML 元素,然后隐藏该对象。

尽管它对我说,但它运行不正常

未捕获的类型错误:对象 [object Object] 没有方法“nothover”

为什么是这样?如果有人可以提供帮助,我将不胜感激。我所说的“帮助”是指解释一些 javascript 函数以及如何确保它们正常运行。

谢谢

javascript jquery settimeout

1
推荐指数
1
解决办法
6802
查看次数

将 set_timeout 与 Sublime Text 3 API 一起使用

我正在尝试在插件中使用(如标题所示)sublime text 3 的 set_timeout 函数。
据我了解,在许多情况下需要使用 lambda 函数。所以我尝试了这个简单的测试:

class SetTimeoutTestCommand(sublime_plugin.WindowCommand):
    def run(self):
        for x in range(1,10):
            sublime.set_timeout(lambda : print(x), 4000)
Run Code Online (Sandbox Code Playgroud)

所以我希望我一次打印一个数字,每个数字之间有 4 秒的延迟。如 Sublime 3 API 中所述:

在给定的延迟(以毫秒为单位)后在主线程中运行回调。具有相同延迟的回调将按添加顺序运行。

但相反,我有 9 '9' 在 4 秒后打印。因此,基于循环的第一次迭代,同时打印所有 '9'。
你知道我能做些什么来解决这个问题吗?

提前致谢 !

编辑:我发现这是有效的(嗯,打印 '9' 9 次,每个之间有 1 秒的延迟:

class SetTimeoutTestCommand(sublime_plugin.WindowCommand):
    def run(self):
        for x in range(1,10):
            sublime.set_timeout(lambda : print(x), x*1000)
Run Code Online (Sandbox Code Playgroud)

但问题仍然存在:它只打印出 '9' ....

python settimeout sublimetext3 sublime-text-plugin

1
推荐指数
1
解决办法
1065
查看次数

取消/停止超时功能

我有一些 javascipt (jQuery),当单击按钮时,我在 #myDiv 中淡出,然后在 5 秒后使用超时函数再次淡出。它工作正常,但如果用户在我的超时内的淡入淡出功能运行之前再次单击该按钮,我需要超时停止并基本上重新开始。

我认为答案是首先运行一个函数来清除超时,但我无法完成这项工作。

$('button').on('click', function() {

  //need function here to stop the timeout if running

  $("#myDiv").fadeIn(slow);

  setTimeout(function(){
    $("#myDiv").fadeOut(slow);
  }, 5000);

});
Run Code Online (Sandbox Code Playgroud)

javascript settimeout

1
推荐指数
1
解决办法
4418
查看次数

如何使用 setInterval 或 setTimeOut 同步执行?

我有一个功能如下:

function foo(args1, args2, retry)
{
    if (retry <= 0)
        return false;

    var isDone = callAnotherFunction(args1, args2);

    if(!isDone) {
       setInterval(function () {
           foo(args1, args2, retry-1);
       },
       2000);
    }
    else
      return true;
}
Run Code Online (Sandbox Code Playgroud)

所以我不确定上面的实现是否正确。但是我需要在另一个函数中使用这个函数。并在 if 块中使用上述函数来决定是否需要执行其他语句。下面是上述函数的用法。

function useIt(args1, args2)
{
    // Other code
    let store = function() {
       if(!foo(args1, args2, 5)) {
           cleanStorage(args1, args2);
           return;
       }
}
Run Code Online (Sandbox Code Playgroud)

所以问题出在函数上useIt(),如果我使用or ,cleanStorage()则不会等待foo()执行。那么我需要如何实现这个功能呢?请帮助我。setIntervalsetTimeOutfoo()

javascript settimeout setinterval node.js promise

1
推荐指数
2
解决办法
4233
查看次数

如何使用 async 或 promise 添加带有 setTimeout 的随机延迟

如果我想使用 setTimeout 和delay = Math.random() * 1000. 由于异步编程和事件循环,答案将是从 1 到 10 以随机顺序排列的数字。

我想要的是以上述相同的延迟按递增顺序打印数字。这可以通过 Promises 或 Async 模块来完成。我的意思是说它应该只打印一次,然后打印 2,依此类推。

任何帮助,将不胜感激。

注意:请不要给出诸如为变量添加时间并将该变量用作延迟之类的答案。

javascript asynchronous settimeout es6-promise

1
推荐指数
1
解决办法
2164
查看次数

为什么我不能直接在 SetTimeout 函数中为 Promise 调用 resolve()

我有两个版本的睡眠功能,一个正在等待解决,另一个不是:

function sleephelper1(ms) {
    return new Promise(function(resolve) {
        setTimeout(() => resolve('done'), ms);
    })
  }

  function sleephelper2(ms) {
    return new Promise(function(resolve) {
        setTimeout(resolve('done'), ms);           
    })
  }
Run Code Online (Sandbox Code Playgroud)

然后我调用 sleephelper1 或 sleephelper2:

async function test(){
var test = await sleephelper1(3000);
console.log(test)
console.log("exit test function")
  }

test()
Run Code Online (Sandbox Code Playgroud)

第一个在解析前等待 3 秒。但是 sleephelper2 无法正常工作。代码立即执行。我认为 SetTimeout 可以将函数的调用延迟给定的时间。resolve() 不是函数吗?我发现这篇文章JavaScript 承诺用 setTimeout 解决,这正是我在这里问的,除了我使用的是异步等待。我也没有得到解释。有人可以向我解释为什么会这样吗?

javascript settimeout resolve node.js async-await

1
推荐指数
1
解决办法
87
查看次数

我如何每秒求和数组元素

我正在尝试按秒总结元素数组。我的数组有 20 个元素,应该在 20 秒内汇总。

setTimeoutfor循环中使用但它不起作用,循环在第一秒之前完成。无论如何要实现?

for (var o = 0; o < 20; o++) {
   setTimeout(function () {
       tempPsum += array[o];
   }, 1000);
}
Run Code Online (Sandbox Code Playgroud)

javascript settimeout

1
推荐指数
1
解决办法
147
查看次数

JavaScript:为什么递归不会停止?

如果我foo++用 a换行,为什么递归不会停止setTimeout?我很确定我错过了有关异步操作的主要 JavaScript 概念。

let foo = 0;

const bar = () => {
  setTimeout(() => foo++);

  if (foo <= 2) {
    bar();
  }
}

bar();
Run Code Online (Sandbox Code Playgroud)

javascript recursion asynchronous callback settimeout

1
推荐指数
1
解决办法
65
查看次数

promise chain does not wait until other promise is resolved

I would like to execute functions one at a time and call another function when a function is finished. I was able to do it using callbacks but not using a promise chain. Here is what I tried (based on https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise#chained_promises) but it executes the first 3 functions at the same time instead of waiting 1 second in each function:

function displayAll() {
    var myPromise = (new Promise(display1))
    .then((new Promise(display2))
    .then((new Promise(display3))
    .then(display4)));
}

function display1(resolve) {
    setTimeout(function () …
Run Code Online (Sandbox Code Playgroud)

javascript settimeout promise

1
推荐指数
1
解决办法
70
查看次数