0

I'm getting an 'Unexpected token if' in the code below. I can't figure out why, the code looks legitimate to me.

var count = 30;
var counter = count;
var testinterval;

testinterval = setInterval(function() {
  $('.counter').text(counter.'s');
  if (counter == 0) {
    testFunction();
    counter = count;
  } else {
    counter--;
  }
}, 1000);

I've searched hi and low for an answer, maybe I'm just being blind.

Knappster
  • 33
  • 5

1 Answers1

4

Opposite to PHP, in JavaScript string concatenation should be done with +:

$('.counter').text(counter + 's');
VisioN
  • 143,310
  • 32
  • 282
  • 281
  • Thanks, I've spent too long doing PHP today and my brain isn't switching well. – Knappster May 20 '13 at 12:52
  • @user1285642 Yeah, it is rather common. – VisioN May 20 '13 at 12:53
  • I've never understood why PHP uses a dot for concatenation - in maths a dot operator would mean _multiply,_ not _add._ (And yes, I know PHP != maths, but still...) – nnnnnn May 20 '13 at 12:57
  • @nnnnnn I personally don't understand that either. Keeping in mind that PHP syntax was mostly derived from C/C++, where concatenation for STL `string` container is also done via `operator+`, I can't get the choice of `.`. – VisioN May 20 '13 at 13:00
  • The only advantage I can think of is that you can type a `.` without needing the `shift` key. – nnnnnn May 20 '13 at 13:01
  • @nnnnnn In my desktop PC I use additional keyboard for `+`, `*` and `/` :) By the way, there was already a question raised about that: http://stackoverflow.com/questions/4266799/why-is-the-php-string-concatenation-operator-a-dot. – VisioN May 20 '13 at 13:03
  • 1
    Thanks for that link, though the people who answered there seem just a little too committed to the "+ couldn't possibly work" view. By the way, **. 1 to you.** – nnnnnn May 20 '13 at 13:21