-
Jun 2nd, 2016, 08:09 AM
#1
Send periodic ajax with ....
Hey all,
I have a little Arduino board that is hosting a couple web pages. From the main page, I get information from the board to the page using a periodic ajax call like this...
Code:
function ajaxRequest()
{
//create the request
var request = new XMLHttpRequest();
request.onreadystatechange = function()
{
if (request.readyState==4 && request.status==200 && request.responseXML!=null)
{ //we got the good stuffs...
//parse it out...
parseAJAX(request.responseXML);
}
}//done with the callback
//send a new request...
var requestString=(AJAX_HEADER + "&" + Math.random());
request.open("GET",requestString,true);
request.send();
//set the refresh...
setTimeout("ajaxRequest()",1000);
}
This works fine.
On the page I also have a button. When the user clicks it, I need to send information back to the server. So I need to send a periodic ajax request and I need to send a request on demand (asyncronously if you will).
What is the preferred method of doing this? Is it to create a new request within the button's onClick handler, or should I inject the users request into the periodic request stream?
i am current injecting the request like this, but I'm not sure if it's the right way to do it. Any insight would be appreciated.
Code:
var ajaxRequestString ="";
function ajaxRequest()
{
//create the request
var request = new XMLHttpRequest();
request.onreadystatechange = function()
{
if (request.readyState==4 && request.status==200 && request.responseXML!=null)
{ //we got the good stuffs...
//parse it out...
parseAJAX(request.responseXML);
}
}//done with the callback
//send a new request...
var requestString=(AJAX_HEADER + ajaxRequestString + "&" + Math.random());
request.open("GET",requestString,true);
request.send();
//set the refresh...
setTimeout("ajaxRequest()",1000);
ajaxRequestString="";
}
function button_OnClick()
{
var x= document.getElementById("x").value;
var y= document.getElementById("y").value;
ajaxRequestString="&x=" + x+ "&y=" + y;
}
Thanks
Kevin
Process control doesn't give you good quality, it gives you consistent quality.
Good quality comes from consistently doing the right things.
Vague general questions have vague general answers. A $100 donation is required for me to help you if you PM me asking for help. Instructions for donating to one of our local charities will be provided.
______________________________ Last edited by kebo : Now. Reason: superfluous typo's
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|