1

I am creating two function name call and call2 and putting this two functions in array x. In another function called timer i am trying to call this function every 2 seconds. But its giving error expected an assigment or function call instead show and expression.

And also i dont want this functions to run when i create an array. this my code http://jsbin.com/IMiVadE/2/

function call(name)
{
  console.log("Hello " + name);
}

function call2()
{
  console.log("Hello world");
}

var x = [call("Nakib"), call2()];
var i = 0;

function timer(x, i)
{
  x[i];
  i++;
  if(i<x.length)
     window.setTimeout(timer(x, i), 2000);

}
timer(x, i);

3 Answers 3

1

You have some mistakes in your code:

  • call2() calls the function. Correct: call2 is the reference to the function.
  • x[i] accesses the value (the function reference). You need x[i]() to execute the function.

Here is your code working: http://jsbin.com/IMiVadE/6/edit

sayHello(name) is a function that generates new functions that output a specific "Hello ..." text.

function sayHello(name)
{
  return function () {
    console.log("Hello " + name);
  }
}

function timer(x, i)
{
  x[i]();
  if (i < x.length - 1) {
    setTimeout(function () {
      timer(x, i + 1);
    }, 2000);
  }
}

var x = [sayHello("Nakib"), sayHello("world")];

setTimeout(function () {
  timer(x, 0);
}, 2000);
Sign up to request clarification or add additional context in comments.

Comments

0

setTimeout needs a function without parameter. So you can wrap the recursive call to timer in an anonymous function to fix that:

window.setTimeout(function(){timer(x, i)}, 2000);

Also, the first line in your timer function, consisting only of x[i];, is useless, although it probably isn't the cause of your problem.

Comments

0

The setTimeout function takes a function as a parameter, you are executing the timer function before it is passed and since timer doesn't return anything, undefined is being passed to the timeout;

window.setTimeout(timer(x, i), 2000); // is the same as...
window.setTimeout(undefined, 2000);

It should be;

window.setTimeout(function() { timer(x, i) }, 2000); // or...
window.setTimeout(timer.bind(this, x, i), 2000);

I'm not sure if this is intentional, but you are doing the same thing with your array;

var x = [call("Nakib"), call2()];

This will execute the functions and their results will be stored in the array. Is this what you want?

2 Comments

I dont want its result to be stored in an array but i want that function to be stored so i can call then in my timer() function.
@Nakib Okay, well you need to have another think about how your code works, and take a look at how first class functions work. Come back if you still need help afterwards. I hope this was helpful :)

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.