5

I've written this script to make a TCPIP request to my machine, this works fine.

When I get status = 0 in the switch (0 = card reader not ready) I wrote the code so that an interval after 10 seconds "presses" again the "#print_card" button so that it tries the request again.

Everything works fine but when I press the button again with $('#print_card').trigger("click"); I get the famous error:

Uncaught TypeError: undefined is not a function while using jQuery and the console gives this to the bootstrap.min.js:6 file (which is the original Bootstrap file).

I tried $('#print_card').click(); and nothing changes...

This is my code. If I remove line #439 $('#print_card').trigger("click"); I don't get the error...:

  var interval;
  var status_timer;
  function attendi_e_riprova(txt){
    var sec = 10;

    var interval = setInterval(function(){
      if(sec != 1){
        sec--;
        if(sec == 1){
          sec_word = 'secondo';
        }else{
          sec_word = 'secondi';
        }
        $('#card-modal-body').html('<p>'+txt+'</p>');
      }else{
        clearInterval(interval);

        //THIS IS THE TRIGGER <-----------------------------------
        $('#print_card').trigger("click");
        //THIS IS THE TRIGGER <-----------------------------------

        return false;
      }
    }, 1000);

    $('.close').click(function(){
      clearInterval(interval);
      clearInterval(status_timer);         
    }); 

  }

  var codemaster = '1';
  var chain = 1;
  var shop = 1;

  function scriviCard(){
    //Another function
  }


  $('#print_card').click(function(){
    $('#card-modal-body').html('<p><i class="fa fa-spinner fa-spin"></i> Ricerca del lettore e card, attendere&hellip;</p>');

    var req = '"CWD2";"';

    $.post(base_url()+'config/RichiestaTCPIP', {array:req}, function(status){
      if(status == 'err'){
        console.error('Errore: controller file config.php funzione di linea #117');
      }else{
        console.log('Status: '+status)

        switch(status){
          case '0':
            attendi_e_riprova('<i class="fa fa-ban text-danger"></i> Lettore NON disponibile. Assicurarsi che sia collegato.');
          break;
          case '1':
            attendi_e_riprova('Lettore vuoto. Inserire una card vergine per poterla scrivere.');
          break;
          case '4':
            attendi_e_riprova('<i class="fa fa-check text-success"></i> Card trovata. In attesa di riconoscimento...');
          break;
          case '5':
            attendi_e_riprova('<i class="fa fa-ban text-danger"></i> Card in scrittura. Attendere completamento.');
          break;
          default:
            //Nuova richiesta per la creazione card (per status 2 o 3)
            scriviCard();
            //console.log(status)
          break;
        }
      }         
    })

  })
13
  • Are you loading jQuery before the bootstrap file? Are you waiting the DOM is loaded before using jQuery in your code? Commented Jun 5, 2014 at 9:10
  • 1
    "I've written this script to make a TCPIP request to my machine..." Not nearly that low-level. :-) You're asking the browser to make an HTTP request for you. Commented Jun 5, 2014 at 9:11
  • You are missing a few ; on a few places, like after the clearInterval(t); }) for example Commented Jun 5, 2014 at 9:11
  • 2
    Try using $('#print_card').click(); Commented Jun 5, 2014 at 9:12
  • @MarcoCI jQuery loads first, second the bootstrap and third this code which is not in an external file but in the page itself. Commented Jun 5, 2014 at 9:12

1 Answer 1

4
+50

I think you should try defining your function in a variable before the trigger command! something like this:

window.clickFunction=function(
    $('#card-modal-body').html('<p><i class="fa fa-spinner fa-spin"></i> Ricerca del lettore e card, attendere&hellip;</p>');

var req = '"CWD2";"';

$.post(base_url()+'config/RichiestaTCPIP', {array:req}, function(status){
  if(status == 'err'){
    console.error('Errore: controller file config.php funzione di linea #117');
  }else{
    console.log('Status: '+status)

    switch(status){
      case '0':
        attendi_e_riprova('<i class="fa fa-ban text-danger"></i> Lettore NON disponibile. Assicurarsi che sia collegato.');
      break;
      case '1':
        attendi_e_riprova('Lettore vuoto. Inserire una card vergine per poterla scrivere.');
      break;
      case '4':
        attendi_e_riprova('<i class="fa fa-check text-success"></i> Card trovata. In attesa di riconoscimento...');
      break;
      case '5':
        attendi_e_riprova('<i class="fa fa-ban text-danger"></i> Card in scrittura. Attendere completamento.');
      break;
      default:
        //Nuova richiesta per la creazione card (per status 2 o 3)
        scriviCard();
        //console.log(status)
      break;
    }
  }});

and then when you want to call it use it like this:

$('#print_card').click(window.clickFunction);

or

window.clickFunction.trigger();

NOTE: you have to define the variable as a global variable and MUST be defined before the trigger or click line!

let me know how it goes.

Sign up to request clarification or add additional context in comments.

1 Comment

OK, that worked! I did it this way: window.clickFunction=function{...} and instead of window.clickFunction.trigger(); I just placed a normal javascript clickFunction();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.