繁体   English   中英

延迟后续行

[英]Delay the subsequent lines

我正在尝试延迟执行后续行,例如:

//block of code

if (//some condition) {
   delay(1000)
}

//block of code

我怎样才能做到这一点? 我尝试使用while循环之类的东西:

var del = 0
setTimeOut(function(){del = 1}, 1000)
while (del === 0) {console.log("waiting for 1 second");}

但这导致浏览器崩溃。 那么,有没有一种声明,方法或函数可以干净地做到这一点呢?

注意 :我不想使用setTimeOut调用函数

还是动画延迟之类的东西:

$("#editor img").on("mouseenter",slideInTag)
                .delay(1000)
                .on("mouseenter",slideOutTag);

简短的答案,如果您不想使用settimeout,那么就没有办法。

基于@Rahul Tripathi答案的评论。

设置this为一个变量,使用变量,而不是this

$(selector).on("mouseenter", function(){
    var x = this;
    setTimeOut(function () {slideOutTag(x)}, 1000);
})

无法在javascript中阻止当前线程。

您需要使用setTimeOut。 因此,在您的情况下,它将是:

$("#editor img").on("mouseenter", function(){ 
  setTimeout(function(){slideOutTag()},1000)
}

而且,您必须定义一个slideOutTag函数,该函数将在“ mouseenter”和一秒钟后调用。

如果要在两者之间使用“时间间隔”,则需要每次添加一个功能,如我在此处所示。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM