为什么Javascript不让我关闭我的功能?

时间:2010-08-17 05:40:33

标签: 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上。

有谁知道为什么会这样失败?我不需要关闭我打开的支架吗?

4 个答案:

答案 0 :(得分: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
}

答案 1 :(得分:4)

您没有关闭函数调用的括号。正如Kobi所说,你还需要setInterval的第三个参数。

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

答案 2 :(得分: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);
}

答案 3 :(得分:0)

你的window.setInterval缺少a)在第二行到最后一行的}之后