Jump to content


JavaScript Callback. (solved)


4 replies to this topic

#1 Schoening

    Member

  • Members
  • PipPip
  • 17 posts

Posted 29 October 2012 - 08:02 PM

I have read a few articles about it, but I still don't understand callback correctly I think.
Most articles make it sound like return.

Essentially what I want to do is run Callback functions on my Server to ensure max speed of the processes.

setTimeout and setInterval can be troublesome. And after asking on StackOverflow, they referred me to callback.


I would like a brief explanation perhaps.
Or tell me if I am right in that callback basically runs as fast as it can. Keeping in mind that JavaScript is event-based and asynchronous.

Here is a piece of JavaScript that is not working, perhaps someone can fix it and explain my error.


function test(callback)
{

if (cubePos < 5)
  {
  cube.position.x = cube.position.x + 0.1;
  cubePos = cube.position.x;
  }

  else
  {
   cube.position.x = - 5;
   cubePos = cube.position.x;
  }

	callback.call(test());

  };

It looks like I am calling the function within itself. But I don't know how it is supposed to look with callback.

#2 Schoening

    Member

  • Members
  • PipPip
  • 17 posts

Posted 29 October 2012 - 08:13 PM

I though a little about this.. I could just run a continuous while loop on the server I guess.

#3 Reedbeta

    DevMaster Staff

  • Administrators
  • 5305 posts
  • LocationBellevue, WA

Posted 29 October 2012 - 08:45 PM

I'm confused. Is all this Javascript code running on the server, using node.js or suchlike? Javascript is usually a client-side language.
reedbeta.com - developer blog, OpenGL demos, and other projects

#4 TheNut

    Senior Member

  • Moderators
  • 1697 posts
  • LocationThornhill, ON

Posted 29 October 2012 - 09:46 PM

The purpose of a callback is to execute another function. In your case, you are recursively calling the test() function as an argument to the callback function, which will obviously lead into a stack overflow and deadlock the app. The whole point of the callback variable is to do something like this:

function test (call_me_back)
{
call_me_back();
// or
call_me_back.call("arg1", "arg2"); //<-- call_me_back function has "this" instance as "owner"
// or
call_me_back.apply(this, "arg1", "arg2"); // <-- Like call() but you can specify the owner.
}

// This is equivalent to what your code does
function test ()
{
	 test();
}

If I were to wager a guess, it looks like you want to create a runtime loop. If you're writing HTML5 compliant code, you should look into window.requestAnimationFrame (API varies on browser - hooray for standards!). If the browser doesn't support that, then setTimeout really is your next best option.

If this is about Node.js, then I have no idea. IMO write a server using a real language :) (/troll).
http://www.nutty.ca - Being a nut has its advantages.

#5 Schoening

    Member

  • Members
  • PipPip
  • 17 posts

Posted 29 October 2012 - 09:59 PM

View PostTheNut, on 29 October 2012 - 09:46 PM, said:

If this is about Node.js, then I have no idea. IMO write a server using a real language :) (/troll).

Haha!

Thanks for the help.

The server runs on NodeJS indeed ;)

I think I have simply read more about JavaScript loops. And general Game AI loops.





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users