0

I was proofreading this function with JSFiddle, and it said there is a missing semicolon on the second line, however, I cannot determine where it would go.

function debtSpiral() {
  var debtID = setInterval(debtIncrease() { //Sets variable for loop stopping later
    debt = (debt * 0.10) + debt; //Increases debt by ten percent...
    document.getElementById("debt").innerHTML = "You owe " + debt + " click(s)"; //Display debt
  }, 60000); //...per minute
  if (debt < 1) { //If you owe less than 1 click
    clearInterval(debtID); //Stop 
  }
}

In addition, JSFiddle is reporting that it "expected an assignment or function call and instead saw an expression" on the fifth line. Can you explain this and how to fix it?

Lastly, why would I need to use (end) on the ninth line?

The fiddle in question (press "JSHint" to see the errors)

double-beep
  • 5,031
  • 17
  • 33
  • 41

3 Answers3

1

This is just a syntax error:

var debtID = setInterval(debtIncrease() { //Sets variable for loop stopping later

I don't know what to advise as a replacement as it really just doesn't make sense. The setInterval() function takes either a string (don't do that) or a function reference as its first parameter. Your code is neither of those; it's not really anything. It's a function call followed by a block, which in this context is not valid.

Maybe you meant (as a comment on this answer suggests)

var debtID = setInterval(function debtIncrease() { // ...
Pointy
  • 405,095
  • 59
  • 585
  • 614
  • 2
    `setInterval(function() {` – Popnoodles Jan 01 '14 at 23:07
  • @Popnoodles it's perfectly OK to put a function name after the `function` keyword – Pointy Jan 01 '14 at 23:14
  • Oh yes, of course, it assigns and executes. It's not the usual thing to do in that given instance though. – Popnoodles Jan 01 '14 at 23:18
  • @Popnoodles well a name in that case just provides a name in case the (otherwise anonymous) function wants to call itself recursively. It's also nice because you get a function name in stack traces instead of just "anonymous". It doesn't have anything to do with assignment, really, because it doesn't (shouldn't) affect the enclosing scope. – Pointy Jan 01 '14 at 23:21
1

You forgot a that anonymous functions don't have names:

var debtID = setInterval(function () {

When the parser complains about misplaces semicolons or whatever else, it usually isn't a semicolon it actually needs, there is just an error in the code and it can't figure out how to parse it.

Damien Black
  • 5,579
  • 18
  • 24
0

As others have answered, you are attempting to call a named function rather than create an anonymous one. Fix it with setInterval(function() {});

However, I have noticed something that might be of interest.

Naming an anonymous function expression

It's possible to give an anonymous function expression a name, so that you may call it later: (function foo() {}), and now foo is available within that closure. However, there can be problems with that, explained in kangax's excellent article.

This isn't a function declaration though, which we all know to be ok and perfectly fine and morally right: function foo() {}. Notice the lack of surrounding parens.

Henry Blyth
  • 1,700
  • 1
  • 15
  • 23