2011-11-23 192 views
2
function moveto(coordinates) { 
    step1 = coordinates[0]; 
    step2 = coordinates[1]; 
     var w = $(document).width(); 
     var h = $(document).height(); 

    $("#full") 
    .animate({left: -(step1 * w)}, 2000) 
    .animate({top: -(step2 * h) }, 500); 
    } 
    var randomNum = randomNumbers(); 
    moveto(randomNum); 

我需要添加此功能的启动延时和重播一定时间后添加启动延迟

+0

这个问题是非常相似的:http://stackoverflow.com/questions/2939980/jquery-how-to-sleep-or-delay 基本上使用'delay()'(http://api.jquery.com/delay /) – andrewmu

回答

2

更改此:

var randomNum = randomNumbers(); 
moveto(randomNum); 

到:

function doRandomMove(){ 
    var randomNum = randomNumbers(); 
    moveto(randomNum); 
} 
setTimeout(function(){ 
    doRandomMove() 
    setInterval(doRandomMove, 10 * 1000); 
}, 1000); 

这将首先调用以1秒的变化,并保持第一呼叫之后10秒后重复。

+0

Tnks像一个魅力:) –

1

试试这个window.setTimeout("moveto(1,2)", 1000);

您的功能将被延迟1000ms调用。

+0

也使用'setTimeout(moveto(randomNum),delay)'而不是'moveto(randomNum);'在函数末尾 – PeterT

+0

不能正确设置'setTimeout(“moveto(randomNum)延迟)'避免直接调用函数? – rekire

+0

它应该是'setTimeout(function(){moveto(randomNum);},delay)'。你说得对,'moveto(randomNum)'会立即调用函数,但传递一个字符串是不好的习惯。改为传递一个匿名函数。 –