P
P
Pavel2015-11-10 00:45:11
JavaScript
Pavel, 2015-11-10 00:45:11

How to execute a function which is a parameter of another function at the right time in javascript?

Hello, my dear javascript guru
I have this code:

var level = parseRoomName(roomId).level;
    if (!svgAction.isExisted(level)) {
      svgAction.ajaxLoad(level);
      loadSvg.success(function(){
        svgAction.enablePanZoom(level);
        // 
        findRoom(roomId);
      })
    } else {
      if (lastSvgLevel == level) {
        // 
        findRoom(roomId);
      } else {
        svgAction.enablePanZoom(level);
        // 
        findRoom(roomId);
      }
    }

to make it easier to read - I will rewrite, removing the excess
if (iHaveNoCar) { // у меня нет машины
      buyCar(); // ajax-запрос на покупку машины
      buyingCar.success(function(){ // машина купилась успешно и теперь она у меня
        // заводим машину
        start(car);
        // едем искать Эльдорадо
        findEldorado();
      })
    } else {// у меня есть машина
      if (carIsStarted) { // машина заведена
       // чего же мы ждем? Поехали искать Эльдрадо
       findEldorado();
      } else {  // двигатель не запущен
        start(car);
        // едем искать Эльдрадо
        findEldorado();
      }
    }


So, a piece of code with the search for El Dorado, which is preceded by buying a car and starting the engine, is called more than once during the course of the program.

It would be great to write a function whose parameter was to search for Eldorado, refuel a car, paint a car, or check a car for bullet holes and also in the left door.

But when I write a function

function initCar(myFunc) {
if (iHaveNoCar) {
      buyCar();
      buyingCar.success(function(){
        start(car);
        myFunc;
      })
    } else {
      if (carIsStarted) {
       myFunc;
      } else { 
        start(car);
        myFunc;
      }
    }
}


and when I run it like this: it first searches for holes in the left door, and only then checks for a car, buys it, delivers it, and starts the engine. How to execute findBulletInDoor('left') inside initCar like it would be done in the first car code example?

initCar(findBulletInDoor('left'))

Answer the question

In order to leave comments, you need to log in

1 answer(s)
P
Pavel Virsky, 2015-11-10
@Carduelis

Replace in InitCar function

myFunc;
on the
myFunc();
and call like this:
initCar(function(){
    findBulletInDoor('left');
});

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question