Author Topic: AJAX help  (Read 544 times)

Offline Zeggy

  • Global Moderator
  • Level 35
  • *****
  • Posts: 1,187
  • Reputation: +13/-4
    • View Profile
AJAX help
« on: October 17, 2008, 11:39:36 AM »
Need help on a little ajax problem...

Right now I am using AJAX calls to a certain page. Each time a keyboard button is pressed, a new AJAX request is made.

However, I would like it so that only one key can be pressed at a time, and only one request is sent even if the keyboard button is held down.

How can I check that no AJAX call has already been made? Or how else can I solve this problem?


Here's the code I'm using:


Code: [Select]
if (navigator.appName.indexOf('Netscape')!=-1) document.captureEvents(Event.KEYDOWN);
document.onkeydown=function(e) {
e=document.all?event.keyCode:e.which;
switch (e) {
case 37: e='left';break;
case 38: e='up';break;
case 39: e='right';break;
case 40: e='down';break;
case 104: e='up';break;
case 100: e='left';break;
case 102: e='right';break;
case 98: e='down';break;
default: e=String.fromCharCode(e);break;
}
get(e)
}

   var http_request = false;
   function makeRequest(url, parameters, divid) {
      http_request = false;
      if (window.XMLHttpRequest) { // Mozilla, Safari,...
         http_request = new XMLHttpRequest();
         if (http_request.overrideMimeType) {
          // set type accordingly to anticipated content type
            //http_request.overrideMimeType('text/xml');
            http_request.overrideMimeType('text/html');
         }
      } else if (window.ActiveXObject) { // IE
         try {
            http_request = new ActiveXObject("Msxml2.XMLHTTP");
         } catch (e) {
            try {
               http_request = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e) {}
         }
      }
      if (!http_request) {
         alert('Cannot create XMLHTTP instance');
         return false;
      }
     
      http_request.open('GET', url + parameters, true);
     
      http_request.onreadystatechange = function() {
    if (http_request.readyState == 4) {
       if (http_request.status == 200) {
  //alert(http_request.responseText);
  result = http_request.responseText;
  $(divid).innerHTML = result;
       } else {
  $(divid).innerHTML = 'There was a problem with the request.';
       }
          }
      }
     
      http_request.send(null);
     
   }
   
   function get(direction) {
      var getstr = "?";
      getstr += "action=move&";
      getstr += "dir=" + direction;
      makeRequest('process/move.php', getstr, 'mapdiv');
   }
   
   function load() {
      var getstr = "?";
      makeRequest('process/move.php', getstr, 'mapdiv');
   }


EDIT: I've sort of solved it with a very crude hack: I set a variable 'requested' to true when a connection is started, and while it's true, no other connections can be made. The variable is reset to false when data is received.
It's very buggy, and the whole thing gets stuck if somebody presses a button repeatedly too quickly, so I also set an interval at every 0.5 seconds that will also reset requested back to false.

That's probably a horrible way of solving the problem, so I'd still like some help finding a proper solution :)
Thanks!
« Last Edit: October 17, 2008, 04:11:25 PM by Zeggy »

Offline andy1984

  • Level 1
  • *
  • Posts: 1
  • Reputation: +0/-0
    • View Profile
Re: AJAX help
« Reply #1 on: October 24, 2008, 02:47:42 AM »
Hey have you found a solution?

Maybe you could hold the key thats pressed and call the ajax thing with a javascript timeout event.

 


SimplePortal 2.3.3 © 2008-2010, SimplePortal