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.