我需要一些建议如何在我的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>
非常适合快速简单的答案!此致敬意
看起来你想使用 $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);
});