2011-11-11 52 views
2

我真的很苦惱這個 - 確定我錯過了簡單的東西。在Javascript中傳遞一個函數作爲參數

下面的代碼: -

function webcamupdate(webcamurl) { 
    alert("I am a function to update the webcam with the url "+webcamurl); 
} 

function countdown(callback) { 
    alert(callback); 
    setTimeout("callback()", 10000); 
} 

var theurl = "THIS IS THE URL"; 

countdown(function(){ webcamupdate(theurl); }); 

正如你所看到的,我想contsruct倒計時功能(有更多的代碼,但是我剛纔發佈的基礎),這將運行一個函數(在這種情況下更新網絡攝像頭)10秒後。

所有工程確定,如果函數在coundown功能沒有參數,但是警報(回調)返回如下: -

功能(){webcamupdate(theurl); }

當webcamupdate函數以「webcamurl」未定義的方式運行時,這當然會崩潰腳本。

什麼警報(回調)實際需要說的是: -

功能(){webcamupdate( 「這是URL」); }

即它在評估參數「theurl」之前通過函數傳遞它。

任何想法,將不勝感激

編輯

我想你我使用警報,而不是一個功能,我也許誤導了(我只是想保持簡單!)

這裏是原始代碼(仍然減少,但給你一個更好的主意)。

function webcamupdate(url) { 
    document.getElementById("cruisecam").src=url+"&rand="+Math.random(); 
    countdown(30,"Webcam",function(){ webcamupdate(url); }); 
} 

function countdown(currentcount,displayelement,callback) { 
    var displaytext; 
    if (currentcount == 0) displaytext = displayelement+" is refreshing now"; 
    else displaytext = displayelement+" updating in "+currentcount+" seconds"; 
    if (trackingcountdown.hasChildNodes()) clearelement("trackingcountdown"); 
    trackingcountdown.appendChild(document.createTextNode(displaytext)); 
    if (currentcount == 0) { 
     countdown(currentcount,displayelement,callback) 
    } 
    else { 
     currentcount--; 
     var countdownparameters=currentcount+',"'+displayelement+'",'+callback; 
     setTimeout("countdown("+countdownparameters+")", 1000); 
    } 
} 

//The function is called on window loading with 
//countdown(30,"Webcam",function(){ webcamupdate(url); }); 

//clearelement is another function (not listed) that clears the node. 

基本上,它是重要的倒數函數 - 它需要3個參數 - 倒計時時間(秒)的DOM元素顯示倒計時,和的函數,在超時運行。

如果第三個參數(函數)不具有其自身的任何參數 - 例如攝像頭() - 它工作正常,但是當我們嘗試添加攝像頭(「webcampic.jpg」)時,由於試圖運行攝像頭(url)而不是攝像頭(「webcampic.jpg」)的功能,它不起作用。

顯然,最簡單的方法就是使URL一個全局變量,但這並不是很好的做法,將使用在其他頁面倒計時代碼通過js文件阻止我。 幫助!

+0

「什麼警報(回調)實際上需要說...」這不是JavaScript的工作原理。 – zzzzBov

+0

那麼當所有的說法和完成時,警報應該說什麼? – arb

回答

3

callback()只是更改爲傳遞函數的引用會做到這一點。

function webcamupdate(webcamurl) { 
    alert("I am a function to update the webcam with the url "+webcamurl); 
} 

function countdown(callback) { 
    alert(callback); 
    setTimeout(callback, 10000); 
} 

var theurl = "THIS IS THE URL"; 

countdown(function(){ webcamupdate(theurl); }); 
0
var theURL = "THIS IS THE URL"; 

function webCamUrlUpdate(url) { 
    alert("I am a function to update the webcam with the url " + url); 
} 

function countDown(callback) { 
    setTimeout(callback, 10000); 
    alert(callback); 
} 

countDown(function() { 
    /* IMPORTANT: need to return that other function */ 
    return webCamUrlUpdate(theURL); 
}); 

你真想要回功能,使setTimeout(callback, 10000);行爲與預期相同。當你用Javascript提醒一個函數時,它和alert(callback.toString())一樣獲得函數頭和主體的完整字符串版本。

http://jsfiddle.net/btleffler/xJ5uw/

有趣的是,如果你在setTimeout()之前提醒的回調,它似乎並不像以往回調火災。如果您註釋掉第一條警報,或者在設置超時後發出警報,則它工作得很好。我只在Chrome中進行測試,所以我必須做更多的研究。

無論哪種方式,在函數被實際調用之前,您都不能使用JavaScript解析(或處理或其他)函數的參數。儘管你可以通過countDown()匿名回調來提醒網址。

+0

謝謝你們 - 我會在週末看看它。不過,我認爲我可能會過度簡化示例並誤導你 - 我會看看是否可以添加更多我的代碼來進一步解釋。 – Clive

0

好夥計 - 我終於自己解決了,這是其他人的解決方案。

首先創建一個返回函數的函數:

function createfunctionwebcamupdate(url) { 
    return function(){ webcamupdate(url); }; 
} 

然後設置一個變量來觸發功能時,它被稱爲:

webcamupdatefunction = createfunctionwebcamupdate(url); 

然後參考這個新變量的回調函數:

function(){ webcamupdatefunction(); } 

Javascript顯然記得存儲在中的變量部分,當變量被調用時。

希望這會有所幫助。

相關問題