0

Im making a function that "calculates" if my clients shop is open or not (so display or not certain content)...so i maked a php file that depending the day it returns 3 values.

1.if this day the shop is "active": 1 or 0 2.the opening time : number 0 to 23 3.the closing time: number 0 to 23.

So i got this so far.

//we make a function to execute in every functionality that needs to know if we are open 
    function isOpen(){
//set the time and 
        var $time = new Date();
        var $horas = $time.getHours();
        var $dia = $time.getDay();
//$abierto variable is the return value, true or false.
        var $abierto;
//$active is the variable we are using to save the ajax return
        var $active;
//setting the names of the day in spanish ;)
        var weekday = new Array(7);
        weekday[0]=  "domingo";
        weekday[1] = "lunes";
        weekday[2] = "martes";
        weekday[3] = "miercoles";
        weekday[4] = "jueves";
        weekday[5] = "viernes";
        weekday[6] = "sabado";

        var $hoy = weekday[$time.getDay()];
        //we send the actual day to the server and get the 3 values 
        $.ajax({
              type: "get",
              url: "http://www.oncelular.com.ar/api_rest/horarios.php",
              dataType: "json",
              crossDomain: true,
              data:{dia:$hoy},
              success: function(response){
//we set it to numbers so they dont evalueate as strings, this is json by the way 
                $active = Number(response.active);
                $open = Number(response.open);
                $close = Number(response.close);
              }
        });
    //okey if the day is active and we are between $open and $close we are open!
        if($active){    
            if($horas >= $open && $horas <= $close ){
                $abierto = true;
            }
        }else{
            $abierto = false;
        }
     //we return the result   
    return $abierto;      
    }

problem: the function ALWAYS return false... i tried a few things and i think the problem is the scoping of the vars inside the succes: function of the ajax call, they are not saving into the globar variables that i set in the beggining. sorry for my bad bad baaad english.

Cœur
  • 37,241
  • 25
  • 195
  • 267
Mijail Dan Cohen
  • 189
  • 1
  • 1
  • 13
  • 3
    The problem is not one of scoping, it's that the `$.ajax()` call is **asynchronous**. – Pointy May 19 '14 at 14:43
  • okey...so how i resolve this? – Mijail Dan Cohen May 19 '14 at 14:53
  • 1
    See the linked duplicate. You can use the Promise mechanism from jQuery or you can put your follow-up code in the "success" callback. Returning a value from an asynchronous operation just doesn't make sense. – Pointy May 19 '14 at 14:54
  • you say that i do all the code inside the `succes` function and return from there the true or false value of the `isOpen()` function? if i cant return a value from the ajax call its not the same problem? – Mijail Dan Cohen May 19 '14 at 15:08
  • ahhhhhhhhhh i think i get it...can i execute a function inside the ajax succes function that sends the parameters to the other function?? – Mijail Dan Cohen May 19 '14 at 15:16
  • Yes! That's exactly the way to do it. That's why in JavaScript it seems like we're always writing code that passes functions around :) – Pointy May 19 '14 at 16:12

0 Answers0