Pergunta

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)

Foi útil?

Solução

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() { // ...

Outras dicas

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.

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.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top