2010-08-17 90 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); 
}