Answer the question
In order to leave comments, you need to log in
Ajax ignores the condition and is executed anyway. Why?
I redid the code a little, there are in fact 3 Ajax requests in one file, 1 of them is constantly executed (every 5 seconds it gives out some text with the possibility for the user to insert) and the remaining 2 to change the I / O language for the dictionary. (Yes, I do a universal an online dictionary and I want it to work in both directions), and the joke is that I seem to have separated them by a condition for execution + made a bulk, but when you switch the language (in fact, another Ajax query is already plowing that replaces the sql query with the reverse one) then it ignores the condition for the failure of the first one and in fact turns into a mess (requests can be executed in both directions). I would like, if the logic of code execution is at least a little clear, to find out what my mistake is (in writing code, you can find a lot of them).
THE CODE:
let boolfalse = false;
function rusAjax(){
if(boolfalse == false)
{
let timeout;
let xhr;
$('.text').on('input', function(e){
e.preventDefault();
let d = $(this).val();
clearTimeout(timeout);
if(xhr)
xhr.abort();
timeout = setTimeout(function(){
xhr = $.ajax({
url: 'php/result.php',
type: 'post',
data: 'text=' + d,
dataType: 'html',
success: function(res) {
$('#result').html(res).fadeIn(300);
/*открыть панель аббревиатуры если поле ввода пустое*/
let audbtn = document.querySelectorAll('.fa-microphone');
let vol = document.querySelectorAll('.vol');
let tekst = document.querySelectorAll('.hide');
let visible = document.querySelectorAll('#visible');
let bool = false;
for(let i=0;i<audbtn.length;i++)
{
/*если есть тег для аудио то выводить в результат*/
if(audbtn[i].contains(audbtn[i].querySelector('audio')))
{
audbtn[i].onclick=function()
{
if(this.querySelector('audio').paused)
{
this.querySelector('audio').play();
}else{
this.querySelector('audio').pause();
}
}
}
}
for (let i = 0; i < visible.length; i++)
{
if(visible[i].contains(visible[i].querySelector('#allWord')))
{
document.querySelector("#copyWord").onclick = function(){
document.querySelector(".vol").classList.add('hint');
document.querySelector(".vol").classList.add('hint--right');
document.querySelector(".vol").setAttribute("data-hint","Скопировано");
function copytext(el)
{
let $tmp = $("<input>");
$("body").append($tmp);
$tmp.val($(el).text()).select();
document.execCommand("copy");
$tmp.remove();
}
copytext("#visible");
}
}
}
}
})
}, 800);
$('#result').fadeOut(200);
})
}
};
rusAjax();
function digAjax(){
if(boolfalse == true)
{
let timeout;
let xhr;
$('.text').on('input', function(e){
e.preventDefault();
let t = $(this).val();
clearTimeout(timeout);
if(xhr)
xhr.abort();
timeout = setTimeout(function(){
xhr = $.ajax({
url: 'php/result2.php',
type: 'post',
data: 'text=' + t,
dataType: 'html',
success: function(res) {
$('#result').html(res).fadeIn(300);
/*открыть панель аббревиатуры если поле ввода пустое*/
let audbtn = document.querySelectorAll('.fa-microphone');
let vol = document.querySelectorAll('.vol');
let tekst = document.querySelectorAll('.hide');
let visible = document.querySelectorAll('#visible');
let bool = false;
for(let i=0;i<audbtn.length;i++)
{
/*если есть тег для аудио то выводить в результат*/
if(audbtn[i].contains(audbtn[i].querySelector('audio')))
{
audbtn[i].onclick=function()
{
if(this.querySelector('audio').paused)
{
this.querySelector('audio').play();
}else{
this.querySelector('audio').pause();
}
}
}
}
for (let i = 0; i < visible.length; i++)
{
if(visible[i].contains(visible[i].querySelector('#allWord')))
{
document.querySelector("#copyWord").onclick = function(){
document.querySelector(".vol").classList.add('hint');
document.querySelector(".vol").classList.add('hint--right');
document.querySelector(".vol").setAttribute("data-hint","Скопировано");
function copytext(el)
{
let $tmp = $("<input>");
$("body").append($tmp);
$tmp.val($(el).text()).select();
document.execCommand("copy");
$tmp.remove();
}
copytext("#visible");
}
}
}
}
})
}, 800);
$('#result').fadeOut(200);
})
}
};
$(".langs").on('change', function(){
$(document).ready(function(){
let arr = [];
//remove attr = $("div").removeAttr("id")
if( $(".rus_lang").prop("selected") )
{
boolfalse = false;
$(".rus_lang").removeAttr("selected");
$(".dig_lang").removeAttr("selected");
$(".rus_lang").attr("selected","selected");
rusAjax();
}else{
}
if( $(".dig_lang").prop("selected") ){
boolfalse = true;
alert("DIG");
$(".rus_lang").removeAttr("selected");
$(".dig_lang").attr("selected","selected");
digAjax();
}
/*
*/
/*скрыть/показать панель аббревиатуры*/
$('i.fa-th').click(function(){
$('#hide').slideToggle(300);
return false;
});
});// ajax
}); //langs change
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question