由于AJAX函数在main函数之后执行, pos 总是返回值0并且无法获取实际值。
如何在主函数
之前执行ajax函数 var pos = 0;
jQuery(function ($) {
jQuery.ajax({
url: wpws_ajaxurl,
type: "POST",
dataType: 'json',
cache: false,
data: {
action: 'getData',
key: Id
},
success: function(pos){
pos = JSON.parse(pos)
},
error: function (jqXHR, text, status) {
alert(JSON.stringify(jqXHR) + " Error");
}
});
});
/*main function starts here*/
jQuery(function ($) {
if (pos)
var timer = setInterval(function () {
if (typeof demo !== "undefined") {
demo.setCurrentTime(pos);
clearTimeout(timer);
}
}, 1000);
});
拜托,有人可以帮我吗?
答案 0 :(得分:0)
两者看起来都很重要。使用名称保持您的AJAX特定功能。例如:
function MyAJAX() {
//your AJAX Function
}
在main函数内调用MyAJAX()
。它将提供所需的POS
值。
/ 主要功能从这里开始 /
jQuery(function ($) {
MyAJAX()
if (pos)
var timer = setInterval(function () {
if (typeof demo !== "undefined") {
demo.setCurrentTime(pos);
clearTimeout(timer);
}
}, 1000);
});
答案 1 :(得分:0)
您可以使用jQuery Promises来完成此任务。
import flash.events.Event;
import flash.system.fscommand;
import flash.events.MouseEvent;
stage.displayState = StageDisplayState.FULL_SCREEN;
var GameMode:Number=0;//Variable Setup
var Planet:Number=0;//
swslogo.alpha = .0;//Game Menu
voidlogomenu.alpha = .0;
newgame.alpha = .0;
loadgame.alpha = .0;
exitgame.alpha = .0;
yes.alpha = .0;
no.alpha = .0;
exitprompt.alpha = .0;
MenuExitFade.alpha = .0;
SwsLogoCorner.alpha = .0;
var IntroTimer = new Timer(3000,1); //add a 3 second timer
var QuitTimer = new Timer(1000,1); //add a 1 second timer
var IntroTimer2 = new Timer(5000,1); //add a 5 second timer
IntroTimer.addEventListener(TimerEvent.TIMER_COMPLETE,swsfadein);
intro();
function intro():void
{
IntroTimer.reset()
IntroTimer.start();
for (var i:int = 0; i < 50; i++)
{
var aStar:DisplayObject = new stars;
MenuSpawner.addChild(aStar);
}
for (var i:int = 0; i < 50; i++)
{
var aStarDust:DisplayObject = new Stardust;
MenuSpawner.addChild(aStarDust);
}
}
function swsfadein(e:Event=null):void
{
IntroTimer.stop();
IntroTimer.removeEventListener(TimerEvent.TIMER_COMPLETE,swsfadein);
swslogo.addEventListener(Event.ENTER_FRAME, fadeinsws)
}
function fadeinsws(e:Event=null){
if(swslogo.alpha >= 1){
swslogo.removeEventListener(Event.ENTER_FRAME, fadeinsws);
IntroTimer2.addEventListener(TimerEvent.TIMER_COMPLETE,swsfadeout);
IntroTimer2.reset()
IntroTimer2.start();
} else {
swslogo.alpha +=0.1;
}
}
function swsfadeout(e:Event=null):void
{
IntroTimer2.stop();
IntroTimer2.removeEventListener(TimerEvent.TIMER_COMPLETE,swsfadeout);
swslogo.addEventListener(Event.ENTER_FRAME, fadeoutsws)
}
function fadeoutsws(e:Event=null){
if(swslogo.alpha <=0){
swslogo.removeEventListener(Event.ENTER_FRAME, fadeoutsws);
IntroTimer.addEventListener(TimerEvent.TIMER_COMPLETE,voidfadein);
IntroTimer.reset()
IntroTimer.start()
} else {
swslogo.alpha -=0.1;
}
}
function voidfadein(e:Event=null):void
{
IntroTimer.stop();
IntroTimer.removeEventListener(TimerEvent.TIMER_COMPLETE,voidfadein);
voidlogomenu.addEventListener(Event.ENTER_FRAME, fadeinvoid)
}
function fadeinvoid(e:Event=null){
if(voidlogomenu.alpha >= 1){
voidlogomenu.removeEventListener(Event.ENTER_FRAME, fadeinvoid);
IntroTimer2.addEventListener(TimerEvent.TIMER_COMPLETE,menufadein);
IntroTimer2.reset()
IntroTimer2.start();
} else {
voidlogomenu.alpha +=0.1;
}
}
function menufadein(e:Event=null):void
{
IntroTimer2.stop();
IntroTimer2.removeEventListener(TimerEvent.TIMER_COMPLETE,menufadein);
newgame.addEventListener(Event.ENTER_FRAME, fadeinnewgame);
loadgame.addEventListener(Event.ENTER_FRAME, fadeinloadgame);
exitgame.addEventListener(Event.ENTER_FRAME, fadeinexitgame);
SwsLogoCorner.addEventListener(Event.ENTER_FRAME, fadeinswslogocorner)
}
function fadeinnewgame(e:Event=null){
if(newgame.alpha >= 1){
newgame.addEventListener(MouseEvent.CLICK, newgameclicked);
newgame.removeEventListener(Event.ENTER_FRAME, fadeinnewgame);
} else {
newgame.alpha +=0.1;
}
}
function fadeinloadgame(e:Event=null){
if(loadgame.alpha >= 1){
loadgame.addEventListener(MouseEvent.CLICK, loadgameclicked);
loadgame.removeEventListener(Event.ENTER_FRAME, fadeinloadgame);
} else {
loadgame.alpha +=0.1;
}
}
function fadeinexitgame(e:Event=null){
if(exitgame.alpha >= 1){
exitgame.addEventListener(MouseEvent.CLICK, exitgameclicked)
exitgame.removeEventListener(Event.ENTER_FRAME, fadeinexitgame);
} else {
exitgame.alpha +=0.1;
}
}
function fadeinswslogocorner(e:Event=null){
if(SwsLogoCorner.alpha >= 1){
SwsLogoCorner.removeEventListener(Event.ENTER_FRAME, fadeinswslogocorner);
} else {
SwsLogoCorner.alpha +=0.1;
}
}
function newgameclicked(e:MouseEvent):void
{
yesnofadein();
exitprompt.addEventListener(Event.ENTER_FRAME, exitpromptfadein)
exitprompt.gotoAndStop(2)
yes.addEventListener(MouseEvent.CLICK, createnewgame)
removemenufunctions();
}
function loadgameclicked(e:MouseEvent):void
{
removemenufunctions();
}
function exitgameclicked(e:MouseEvent)
{
yesnofadein();
exitprompt.addEventListener(Event.ENTER_FRAME, exitpromptfadein)
exitprompt.gotoAndStop(1)
yes.addEventListener(MouseEvent.CLICK, exit)
removemenufunctions();
}
function exitpromptfadein(e:Event=null){
if(exitprompt.alpha >= 1){
no.addEventListener(MouseEvent.CLICK, exitpromptfadeout)
exitprompt.removeEventListener(Event.ENTER_FRAME, exitpromptfadein);
} else {
exitprompt.alpha +=0.1;
}
}
function exitpromptfadeout(e:Event=null):void
{
no.removeEventListener(MouseEvent.CLICK, exitpromptfadeout)
exitprompt.addEventListener(Event.ENTER_FRAME, fadeoutexitprompt);
}
function fadeoutexitprompt(e:Event=null){
if(exitprompt.alpha <=0){
exitprompt.removeEventListener(Event.ENTER_FRAME, fadeoutexitprompt);
} else {
exitprompt.alpha -=0.1;
}
}
function yesnofadein():void
{
yes.addEventListener(Event.ENTER_FRAME, fadeinyes)
no.addEventListener(Event.ENTER_FRAME, fadeinno)
}
function fadeinyes(e:Event=null){
if(yes.alpha >= 1){
yes.removeEventListener(Event.ENTER_FRAME, fadeinyes);
} else {
yes.alpha +=0.1;
}
}
function fadeinno(e:Event=null){
if(no.alpha >= 1){
no.addEventListener(MouseEvent.CLICK, replacemenufunctions)
no.addEventListener(MouseEvent.CLICK, yesnofadeout)
no.removeEventListener(Event.ENTER_FRAME, fadeinno);
} else {
no.alpha +=0.1;
}
}
function yesnofadeout(e:MouseEvent):void
{
yes.removeEventListener(MouseEvent.CLICK, exit)
yes.removeEventListener(MouseEvent.CLICK, createnewgame)
yes.addEventListener(Event.ENTER_FRAME, fadeoutyes)
no.addEventListener(Event.ENTER_FRAME, fadeoutno)
no.removeEventListener(MouseEvent.CLICK, yesnofadeout)
}
function fadeoutyes(e:Event=null){
if(yes.alpha <=0){
yes.removeEventListener(Event.ENTER_FRAME, fadeoutyes);
} else {
yes.alpha -=0.1;
}
}
function fadeoutno(e:Event=null){
if(no.alpha <=0){
no.removeEventListener(Event.ENTER_FRAME, fadeoutno);
} else {
no.alpha -=0.1;
}
}
function removemenufunctions():void
{
exitgame.removeEventListener(MouseEvent.CLICK, exitgameclicked);
loadgame.removeEventListener(MouseEvent.CLICK, loadgameclicked);
newgame.removeEventListener(MouseEvent.CLICK, newgameclicked);
}
function replacemenufunctions(e:Event=null):void
{
exitgame.addEventListener(MouseEvent.CLICK, exitgameclicked);
loadgame.addEventListener(MouseEvent.CLICK, loadgameclicked);
newgame.addEventListener(MouseEvent.CLICK, newgameclicked);
}
function exit(e:MouseEvent)
{
MenuExitFade.x = -35;
MenuExitFade.y = 0;
MenuExitFade.addEventListener(Event.ENTER_FRAME, exitblackfade);
}
function exitblackfade(e:Event)
{
if(MenuExitFade.alpha >= 1){
MenuExitFade.removeEventListener(Event.ENTER_FRAME, exitblackfade);
QuitTimer.addEventListener(TimerEvent.TIMER_COMPLETE,forceclose);
QuitTimer.reset()
QuitTimer.start();
} else {
MenuExitFade.alpha +=0.1;
}
}
function forceclose(e:Event)
{
QuitTimer.stop();
QuitTimer.removeEventListener(TimerEvent.TIMER_COMPLETE,forceclose);
fscommand('quit');
}
function createnewgame(e:MouseEvent)
{
var GameMode=1;
var Planet=1;
MenuSpawner.alpha=0
newgame.alpha=0
loadgame.alpha=0
exitgame.alpha=0
voidlogomenu.alpha=0
yes.alpha=0
no.alpha=0
exitprompt.alpha=0
SwsLogoCorner.alpha=0
yes.removeEventListener(MouseEvent.CLICK, createnewgame)
no.removeEventListener(MouseEvent.CLICK, yesnofadeout)
}
现在你要访问pos的值。
import flash.events.Event;
import flash.system.fscommand;
import flash.events.MouseEvent;
setupplanet();
Foreground.alpha=0
function setupplanet():void
{
Foreground.addEventListener(Event.ENTER_FRAME, PlanetLoop)
}
function PlanetLoop(e:Event=null):void
{
if(GameMode==0){
Foreground.alpha=0
} else {
Foreground.alpha=1
}
Foreground.gotoAndStop(1)
}
答案 2 :(得分:0)
我认为你应该在你的main函数中进行ajax调用,并在“success”ajax函数中检查“pos”值。
答案 3 :(得分:0)
我认为你应该这样做你的ajax请求:
$.ajax({
url: ... ,
type: ... ,
data: ...
}).done(function() {
// change the value of 'pos' if necessary
// call your main function
});
因为done
仅在ajax请求返回
答案 4 :(得分:0)
在ajax调用中添加async:false
解决了我的问题。非常感谢Nishant Solanki:)