17

我如何从中发挥作用?

         //check if station is alive
         $.ajax({
           url: "lib/grab.php",
           data: "check_live=1&stream_url="+valueSelected,
           type: "GET",
               success: function (resp) {
                 if (resp == 1) {
                   play_this(valueSelected);
                 } else {
                   //
                 }
               },
               error: function (e) {
                 console.dir(e);
               }
         });

我以为我可以做这样的事情:

function is_alive(valueSelected) {
result = false;
             //check if station is alive
             $.ajax({
               url: "lib/grab.php",
               data: "check_live=1&stream_url="+valueSelected,
               type: "GET",
                   success: function (resp) {
                     if (resp == 1) {
                       result = true;
                     } else {
                       //
                     }
                   },
                   error: function (e) {
                     console.dir(e);
                   }
             });
return result;
}

但显然由于 ajax 调用的异步性质,结果总是返回 false。

处理这种情况的诀窍是什么?

似乎工作:

        //check if station is alive
        function is_alive(url) {
          //
          var result = false;
          //
          return $.ajax({
            url: "lib/grab.php",
            data: "check_live=1&stream_url="+url,
            type: "GET",
                success: function (resp) {
                  if (resp == 1) {
                    //
                    result = true;
                    //
                  }
                },
                error: function (e) {
                  console.dir(e);
                }
          }).then(function() {
            return $.Deferred(function(def) {
              def.resolveWith({},[result,url]);
            }).promise();
          });
        }

并这样称呼它:

    //Change song on select, works both for fav and station lists
    $(document).on("click", ".ui-listview li a", function(){
      var valueSelected = $(this).data("station-url");
      //
      is_alive(valueSelected).done(function(result,url){
          if (result) {
            //
            play_this(valueSelected);
            //
          }
      });
    });
4

2 回答 2

9

您可以提供 async: false 选项

function is_alive(valueSelected) {
     result = false;
             //check if station is alive
             $.ajax({
               async: false,
               url: "lib/grab.php",
               data: "check_live=1&stream_url="+valueSelected,
               type: "GET",
                   success: function (resp) {
                     if (resp == 1) {
                       result = true;
                     } else {
                       //
                     }
                   },
                   error: function (e) {
                     console.dir(e);
                   }
             });
return result;
}
于 2013-10-10T17:02:43.017 回答
9

您不必使其同步即可使其成为有用的功能。

function is_alive(valueSelected) {
    //check if station is alive
    return $.ajax({
        url: "lib/grab.php",
        data: "check_live=1&stream_url=" + valueSelected,
        type: "GET",
        error: function (e) {
            console.dir(e);
        }
    }).then(function (resp) {
        return $.Deferred(function(def){
            def.resolveWith({},[resp == 1,valueSelected]);
        }).promise();
    });
}

is_alive(somevalue).done(function(result,valueSelected){
    alert(result);
    alert(valueSelected); 
});
于 2013-10-10T17:09:53.030 回答