2011-11-23 193 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)'會立即調用函數,但傳遞一個字符串是不好的習慣。改爲傳遞一個匿名函數。 –