2010-08-17 240 views
1

我不知道這個人,這真的很奇怪,但我可能只是犯了一個簡單的錯誤,甚至沒有意識到它。爲什麼Javascript不讓我關閉我的功能?

我是排序的 JavaScript的新手,所以我試圖編寫一個腳本,從PHP腳本(它只返回一個數字)獲取內容,並將該數據寫入一個div ...但Javascript有其他想法。我正在使用Mac OS X上的Chrome進行測試,但它在Safari上也無法運行。

以下塊給我的問題:

function getContent() { 
window.setInterval(function() { 
    $.get("get.php", function (data) { 
    $('#img').slideUp(); 
    $('#div').html(data); 
    $('#div').slideDown(); 
    } 
} 
} 

這與失敗:

Uncaught SyntaxError: Unexpected token } 

上線51,或行8,這個例子的目的。

有人知道爲什麼它會失敗嗎?我不需要關閉我打開的括號嗎?

+1

恕我直言,你的縮進太小了,很難注意到明顯缺少''''字符。我寧願將它縮進至少兩個空格。但是,這只是我。 – 2010-08-17 05:46:40

+0

對不起......它剛剛出來。 :( – esqew 2010-08-17 06:07:27

回答

10

你的花括號都行,但是你錯過了幾個括號缺少):

function getContent() { 
window.setInterval(function() { 
    $.get("get.php", function (data) { 
    $('#img').slideUp(); 
    $('#div').html(data); 
    $('#div').slideDown(); 
    }); //get - end statement 
}, 4000); // setInterval - need another parameter, end statement 
} 
+0

感謝您的答案。:) – esqew 2010-08-17 06:07:57

4

你沒有關閉函數調用的括號。正如Kobi所說,你還需要第三個參數setInterval

function getContent() { 
window.setInterval(function() { 
    $.get("get.php", function (data) { 
    $('#img').slideUp(); 
    $('#div').html(data); 
    $('#div').slideDown(); 
    }); 
}, 1000); 
} 
0

您window.setInterval是後}在倒數第二行

3

該window.setInterval函數有一個語法如下:

window.setInterval(functionRef, timeout); 

在你的情況setInterval$.get()函數調用缺少結束括號)。你很清楚你可以這樣寫:

function getContent() { 
    // success handler 
    var success = function() { 
    // declare the function first as "changeUI" 
    var changeUI = function() { 
     $('#img').slideUp(); 
     $('#div').html(data); 
     $('#div').slideDown(); 
    }; 
    // call the setInterval passing the function 
    window.setInterval(changeUI, 2000); 
    }; 

    // get the resource and call the "success" function on successful response 
    $.get("get.php", success); 
} 
相關問題