0

A new get ajax request should be created (even if there is any outstanding ajax request) whenever:-

1) i input any string and press enter (with the help of terminal api).

2) and key-event ajax request when i press ctrl^D ( or C, O,etc). [my code]

But after creating numerous (7 to 8) ajax requests (which are still running), i am not able to create new key-event ajax request. What shall be changed in this code?

<script>
jQuery(document).ready(function($) {
  var id = 1;
  document.addEventListener("keyup", function(event) {

    if(event.ctrlKey) {
      switch (event.which) { 
      case 67: 
        input_signal='__%ctrl+C%__';
        break;
      case 68: 
        input_signal='__%ctrl+D%__';
        break;
      case 90: 
        input_signal='__%ctrl+Z%__';
        break;
      }
      /*--------KEY-EVENT AJAX GET REQUEST CODE-------------*/

      $('body').ajaxStart(function(){
        $.get('/terminal_exec',{input: input_signal}, function(){
          //input_signal='process quit');
        });
      });
      console.log( "You pressed "+input_signal );
      event.preventDefault()

      /*------------------------------------------------*/
    }
  })

  $('body').terminal(function(command, term) {
    if (command == 'help') {
      term.echo('yoyo man');
    } else {
      /*--------INPUT STRING AJAX GET REQUEST CODE-------------*/
      $.get('/terminal_exec',{input: command}, function(data){
        term.echo(data);
      });
      /*----------------------------------------------------*/
    }
  }, {
    greetings: "HEY",
    onBlur: function() {
      // prevent loosing focus
      return false;
    }
  });
});

Vibhor Verma
  • 161
  • 1
  • 4
  • 13
  • Are you saying the ajax requests never get sent, even after one of the previous 7 complete/terminate? – Kevin B Jan 11 '17 at 16:15
  • 1
    also... binding events to ajaxStart, on a keyup, is probably a bad idea? that would mean... for each keyup you're binding another event? that will start yet another ajax request? – Kevin B Jan 11 '17 at 16:15
  • You do realize that ajaxStart only gets fired when an ajax request is started while no other ajax requests are pending, right? – Kevin B Jan 11 '17 at 16:18
  • @KevinB yes, even after 1 of the previous 7 requests are completed or terminated – Vibhor Verma Jan 11 '17 at 16:33
  • @KevinB i first used keydown, but it was giving some bug(don't remember) , it made the code work, don't know why :P . – Vibhor Verma Jan 11 '17 at 16:35
  • @KevinB yes, i tried using ajaxStart , it was giving error – Vibhor Verma Jan 11 '17 at 16:36
  • "Whenever an Ajax request is about to be sent, jQuery checks whether there are any other outstanding Ajax requests. If none are in progress, jQuery triggers the ajaxStart event. Any and all handlers that have been registered with the .ajaxStart() method are executed at this time." so after you press key for the 10 time it will execute your function 10 times. – jcubic Jan 11 '17 at 17:16
  • @jcubic can you see what is error in .ajaxStart() method , it is not working as you said :P – Vibhor Verma Jan 11 '17 at 17:35
  • just remove ajaxStart, it's the same as you would call `document.addEventListener("keyup", function() { .. });` after each keyup. It's an event handler that is executed on each ajax call if there are no other ajax calls in progress. – jcubic Jan 11 '17 at 17:44
  • why you're using ajaxStart? – jcubic Jan 11 '17 at 17:44
  • @jcubic the way you are saying i.e. to remove ajaxStart, the code runs perfectly if I send keyevent or input string after 2-3 ajax requests being in progress. But after 8-9 ajax requests are in progress , ajax get request of key-event is not going whereas input string ajax request is working perfectly. – Vibhor Verma Jan 11 '17 at 20:11

0 Answers0