确定AJAX的成功范围

时间:2014-05-19 14:41:11

标签: javascript jquery ajax function scope

我正在制作一个"计算"如果我的客户商店是开放的(所以显示或不是某些内容)...所以我做了一个PHP文件,取决于它返回3个值的日期。

1.如果这一天,商店活跃":1或0 2.开放时间:0到23号 3.结束时间:0到23号。

所以到目前为止我得到了这个。

//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;      
    }

问题:函数总是返回false ...我尝试了一些事情,我认为问题是ajax调用的succes:函数内的变量的范围,它们没有保存到globar变量中我在开始时设定了。 抱歉我的坏巴巴英语。

0 个答案:

没有答案