Reputation: 7144
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…</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;
}
}
})
})
Upvotes: 5
Views: 4537
Reputation: 7207
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…</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.
Upvotes: 4