Good evening, how do I pause the execution of if in javascript?
In the loop we use break. I tried adding a return shortly after the alert (which I will show in the following code) but it gives a reload on the page, I do not know why ...
if (cookie == 1) {
alert("Oi, você enviou uma mensagem a pouco tempo, em breve vamos te responder, ok?");
return;
}
The cookie variable is created above, but it does not matter if you post the cookie here.
Reinforcing my problem once more, I want to pause the execution of this if and exit, but a reload is happening this way and can not continue like this.
Complete code as requested:
if (document.querySelector('.menu-help-box-form')) {
document.querySelector('.menu-help-box-form').addEventListener('submit', function (event) {
var cookie = getCookieValue('form_help_box');
if (cookie == 1) {
alert("Oi, você enviou uma mensagem a pouco tempo, em breve vamos te responder, ok?");
return;
}
event.preventDefault();
var formD = new FormData(this);
var helpBObject = {};
var con = {
0: "name",
1: "email",
2: "reason",
3: "message"
};
var numCont = 0;
formD.forEach(function (element) {
helpBObject[con[numCont]] = element;
numCont++;
});
$.ajax({
url: "/api/dataentities/HB/documents",
type: "POST",
timeout: 0,
headers: {
"Content-Type": "application/json",
"Accept": "application/vnd.vtex.ds.v10+json"
},
data: JSON.stringify(helpBObject),
success: function () {
document.querySelector('.success-send-form-help').style.display = "flex";
var d = new Date();
d.setTime(d.getTime() + (1 * 24 * 60 * 60 * 1000));
var expires = "expires=" + d.toUTCString();
document.cookie = "form_help_box=1; " + expires + "; path=/";
},
error: function (error) {
console.log("Erro Help Box: ", error);
}
});
});
document.querySelector('.cancel-send-form-help').addEventListener('click', function () {
document.querySelector('.menu-help-box-form').style.display = "none";
if (document.querySelector('.success-send-form-help')) {
document.querySelector('.success-send-form-help').style.display = "none";
}
document.querySelector('.menu-help-box-ul').style.display = "block";
});
}
Edited to complement
If the cookie exists (condition passed in if), I want it not to execute whatever is below that if.