2015-04-30 38 views
0

我试图拖延执行后续的行类似:延迟后续线上

//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); 
+1

没有,没有。你*有*使用带有回调函数的'setTimeout'。 – Bergi

+0

为什么你不想使用'setTimeout'? – danguilherme

+2

'或者是否有像''动画延迟':是的,你可以将'setTimeout'转换成Promise,但这只是语法糖 – CodingIntrigue

回答

1

基于@Rahul特里帕蒂答案的评论。

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

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

是的,这是继续前进的方式! +1 –

0

有没有办法来阻止当前线程在JavaScript中。

+0

...因为有在JavaScript没有线程:-) – Bergi

+2

有一个单独的线程,并有工人,是单独的线程... – taxicala

+0

我更新了我的问题 –

1

简短的答案,如果你不想使用settimeout那么有NO方式来做到这一点。

+0

我更新了我的问题 –

+0

@RanaMallah: - 答案仍然是NO(*因为RGraham说你可以使用承诺但会导致语法糖*)。顺便说一句,你为什么不想使用setTimeOut? –

+0

因为用setTimeOut你不能传递像这样的参数 –

0

您需要使用setTimeOut。所以你的情况这将是:

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

而且你必须定义一个slideOutTag功能,这将“的mouseenter”,并在一秒钟后调用。

如果你想有间“时间分隔符”,则需要每次增加一个功能,我发现在这里。

+0

'你需要使用setTimeOut'OP已经明确提到他不想使用setTimeOut –

+0

啊,这在第一个版本中没有。那么,祝你好运。 –