2013-09-25 26 views
-2

我需要一些建議如何保存或使用我的AJAX輸出在我的if(語句)。這就是代碼的樣子,所以我希望你明白我在尋找什麼。IF(聲明)中使用AJAX值...動畫

setInterval("yourAjaxCall()",1000); 
     function yourAjaxCall() 
     { 

     $.ajax({          
      url: 'api.php',     //the script to call to get data   
      data: "",      //you can insert url argumnets here to pass to api.php 
              //for example "id=5&parent=6" 
      dataType: 'json',    //data format 
      success: function(data)   //on recieve of reply 
      { 
      var id = data[0];    //get id 
      var vname = data[1];   //get name 

      $("#square").css({"background-color" : vname}); 


      } 
     }); 
     }; 


    </script> 
</head> 
<body> 
<div id="square" style="width:200px; height:200px; position:relative; "> 
</div> 

<script> 

EDIT $color = output from ajax(background-color of square) //HOW DO I SAVE IT OR USE IT? 

/* Start animation */ 
    setInterval("timedani()",10000); 
    function timedani(){ 
    /* Stop animation when button is clicked */ 
     $("#stop").click(function(){ 
      $("#square").stop(); 

     }); 
    //First TR-bubble 
     //$("#ball").animate({left: 'x', top: 'y'}, 1500); 

    if($color == 'pink'){      // PROBABLY WRONG 
     $("#square").animate({left: 510, top: 75}, 1500); 
     $("#square").animate({left: 0, top: 75}, 1500); 
     } 

    else if($color == 'blue'){    // PROBABLY WRONG 
     $("#square").animate({left: 0, top: 475}, 1500); 
     $("#square").animate({left: 0, top: 0}, 1500); 
     } 


    }; 

    $("#stop").click(function(){ 
     $("#kvadrat1").stop(); 

    }); 

    $("#back").click(function(){ 
     $("#kvadrat1").animate({left: '0px', top: '0px'}, 1500); 

    }); 
</script> 

偉大的快速和簡單的答案!順祝商祺

+0

使用==在,如果像如果($顏色= ='粉紅') – Greenhorn

+0

是的,這很簡單,但主要問題是我如何從ajax得到我的輸出在IF語句中。聽說這是不可能保存爲PHP變量,但必須有另一種方式? @Ram – Hilven

+1

這是JavaScript的「雞或蛋」問題。請閱讀此:http://stackoverflow.com/questions/14220321/how-to-return-the-response-from-an-ajax-call –

回答

0

看起來像你想使用$color作爲一個全局變量,所以它在Ajax回調

setInterval("yourAjaxCall()", 1000); 

function yourAjaxCall() { 

    $.ajax({ 
     url: 'api.php', //the script to call to get data   
     data: "", //you can insert url argumnets here to pass to api.php 
     //for example "id=5&parent=6" 
     dataType: 'json', //data format 
     success: function (data) //on recieve of reply 
     { 
      var id = data[0]; //get id 
      var vname = data[1]; //get name 

      $("#square").css({ 
       "background-color": vname 
      }); 

      $color = vname; 
     } 
    }); 
}; 

然後

//declar $color as a global variable 
var $color;//if you want to assign a default value use var $color = 'pink' 

/* Start animation */ 
setInterval("timedani()", 10000); 

function timedani() { 
    /* Stop animation when button is clicked */ 
    $("#stop").click(function() { 
     $("#square").stop(); 

    }); 
    //First TR-bubble 
    //$("#ball").animate({left: 'x', top: 'y'}, 1500); 

    if ($color == 'pink') { // PROBABLY WRONG 
     $("#square").animate({ 
      left: 510, 
      top: 75 
     }, 1500); 
     $("#square").animate({ 
      left: 0, 
      top: 75 
     }, 1500); 
    } else if ($color == 'blue') { // PROBABLY WRONG 
     $("#square").animate({ 
      left: 0, 
      top: 475 
     }, 1500); 
     $("#square").animate({ 
      left: 0, 
      top: 0 
     }, 1500); 
    } 


}; 

$("#stop").click(function() { 
    $("#kvadrat1").stop(); 

}); 

$("#back").click(function() { 
    $("#kvadrat1").animate({ 
     left: '0px', 
     top: '0px' 
    }, 1500); 

}); 
+0

是的,我知道:P但我需要一種方法來保存從我的$顏色輸出。或者我需要一種方法從#square中檢索我的背景顏色以在我的IF中使用它。 – Hilven

+0

非常感謝,工作正常!祝你有美好的一天! – Hilven