How do I keep my game running when I'm doing a XMLHttpRequest in script?

0 favourites
  • 2 posts
From the Asset Store
Full game Construct 2 and Construct 3 to post on Google Play
  • I'm currently doing a XMLHttpRequest which works fine but holds up the rest of the game until the data is returned.

    How would I keep the game running while the request is going on in the background?

    Current request looks like this:

    function apiPostRequest(request, body) {
    	var xmlHttp = new XMLHttpRequest();
    	xmlHttp.open( "POST", baseApiURL + request, false );
    	xmlHttp.setRequestHeader('Content-Type', 'application/octet-stream');
    	xmlHttp.setRequestHeader('X-Auth-token', token);
    	xmlHttp.send(body);
    	console.log("Resp: "+xmlHttp.responseText);
    	
    	resp=xmlHttp.responseText;
    	
    	return xmlHttp.responseText;
    }
    
  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • SOLVED

    Even if nobody replies, I think that just the act of posting a question here seems to bring about some kind of mystical epiphany.:)

    In case anyone should ever need this; to change this to an asynchronous function which runs while your game is still running, it should be something like this:

    function apiPostRequest(request, body,runtime) {
    	var xmlHttp = new XMLHttpRequest();
    	
    	xmlHttp.open( "POST", baseApiURL + request, true );//TRUE indicates asynchronous
    	xmlHttp.setRequestHeader('Content-Type', 'application/octet-stream');
    	xmlHttp.setRequestHeader('X-Auth-token', token);
    	
    	xmlHttp.onload =function (e)
    	{
    		
    		console.log("Resp: "+xmlHttp.responseText);
    		//alert ("Resp :"+xmlHttp.responseText);
    
    		resp=xmlHttp.responseText;
    		results=JSON.parse(xmlHttp.responseText);
    		
    		runtime.globalVars.returned_data = xmlHttp.responseText;
    		runtime.callFunction("Event_Got_Data");//call event sheet function once complete
    
    	}
    
    	xmlHttp.send(body);
    	
    }
    
Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)