Click here to Skip to main content
15,900,108 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a program that needs to get some fresh data every time a button is pushed, then use that data to continue execution. I have read through a ton of suggestions bit none seem to work so i must be doing something wrong. This is the calling function:
function _onSpin(iNumBettingLines,iCoin,iCurBet) {
    if(iCurBet > s_aSession["iMoney"]){
        _dieError("INVALID BET: "+iCurBet+",money:"+s_aSession["iMoney"]);
        return;
    }
 makeNextResult(s_aSession["user_name"],s_aSession["coin_bet"],s_aSession["coin_bet"]);
	  responce_string = getNextResults();
    	_Result = [];
        var res_string = parseResult(response_string);
        var incomming = res_string.split("#");
        var this_ln;
        var x = 0;
        var bFreespin = 0;
        var bBonus = 0;
        var reel_pos;
        var bet_amt = 25;
        for(i=0;i<=incomming.length-1;i++){
       	   this_ln=incomming[i].split(",");
       	  _Result[i]=this_ln;
        }          
      }
      _initSettings();
      //$(s_oMain).trigger("bet_placed",{bet:iCoin,tot_bet:iCurBet});
      var rls;
      for(a=0;a<=_Result.length-1;a++){
      	if(_Result[0][0]==bet_amt){
      		rls = _Result[0][3];
      		break;
      	}
      }
  		if(rls!="undefined"){
       reel_pos =rls.split("*");
       for(a=0;a<reel_pos.length;a++){
          fs = reel_pos[a].split("%");
          _aFinalSymbols[a] = new Array(); 
          _aFinalSymbols[a][0] = fs[0];
          _aFinalSymbols[a][1] = fs[1];
          _aFinalSymbols[a][2] = fs[2];
          _aFinalSymbols[a][3] = fs[3];
          _aFinalSymbols[a][4] = fs[4];
  		 }
       
      }
       if(_Result[0][1]==0){
          var str = JSON.stringify(_aFinalSymbols);
          //_oInterface.refreshTotalBet();
          return "res=true&win=false&pattern="+JSON.stringify(_aFinalSymbols)+"&money="+s_aSession["iMoney"]+"&freespin="+s_aSession["iTotFreeSpin"]+"&bonus=false&bonus_prize=-1";
       }
   
      aRet = stuffResults(bet_amt);
      var str = JSON.stringify(_aFinalSymbols);
     // _oInterface.refreshTotalBet();
      return "res=true&win=true&pattern="+_aFinalSymbols+"&win_lines="+JSON.stringify(aRet)+"&money="+s_aSession["iMoney"]+
           "&tot_win="+iTotWin+"&freespin="+s_aSession["iTotFreeSpin"]+"&bonus="+s_aSession["bBonus"]+"&bonus_prize="+iPrizeReceived+"&cash="+s_aSession["iSlotCash"] ;
}


notice the line "responce_string = getNextResults();"

Here is the code for getNextResults()

function getNextResults(){
  foo(function(result) {
     return result;
  });
}


And finally the foo function
function foo(result) {
   var url = "http://server678.net/ExampleService.asmx";
    $.support.cors = true;
    $.ajax({
        type: "POST",
        url: url,
        contentType: "text/xml",
        dataType: "xml",
        data: _SoaMessage,
        processData: true, 
        success: function (data, status, req) {
           result(req.responseText);
        }
    });
}


The program calls getnextResult but instead of waining for the function to return a value it just blows right through it and response string is always undefined.

What I have tried:

I have been researching this for days and nothing seems to work. Please help.
Posted
Updated 18-Oct-18 13:28pm
Comments
Mohibur Rashid 18-Oct-18 19:18pm    
Easiest way to do is, disable the button or create flag that says being processed. On return, success or failure, enable the button or clear the flag
F-ES Sitecore 19-Oct-18 5:48am    
You need to either add "async:false" to the ajax options to make the code wait for the results of your ajax call, or re-architect your code to make the code in the "success" event kick off whatever needs to be done with the data.

1 solution

Easiest way to do is, disable the button or create flag that says being processed. On return, success or failure, enable the button or clear the flag.

JavaScript
$("#button_id").click(function(){
 if(this.enabled==false) {
   alert("still processing");
   return;
 }
 this.enabled=false;
 $.ajax(
  {
    url:...,
    success: function(result) {
     ... // Process result
    }, 
    error:function() { 
    },
    complete: function(){
      this.enabled=true
    }
  }
 )
  
})
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900