1

I dunno guys, this is a really weird one, but I might just be making a simple mistake and not even realizing it.

I'm sort of a newbie to Javascript, so I'm attempting to write a script that gets content from a PHP script (which returns only a number) and write that data to a div... but Javascript had other ideas. I'm testing on Chrome on Mac OS X, although it doesn't work on Safari either.

The following block is giving me problems:

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

Which is failing with:

Uncaught SyntaxError: Unexpected token }

on line 51, or line 8, for the purposes of this example.

Does anyone know why it would fail like this? Don't I need to close the brackets I open?

2
  • 1
    IMHO Your indentation is too small, making it difficult to notice the obvious missing ) char. I prefer to indent it to at least two spaces. But that is just me. Commented Aug 17, 2010 at 5:46
  • Sorry... it just came out like that. :( Commented Aug 17, 2010 at 6:07

4 Answers 4

10

Your curly braces are OK, but you're missing a few parentheses:

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
}
Sign up to request clarification or add additional context in comments.

Comments

4

You're not closing the parentheses for your function calls. As Kobi said, you also need a third parameter for setInterval.

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

Comments

3

The window.setInterval function has a following syntax:

window.setInterval(functionRef, timeout);

In your case the setInterval and $.get() function call are missing the closing parentheses ). It would be clear for you to write this in the following way:

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);
}

Comments

0

your window.setInterval is missing a ) after the } on the second to last line

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.