Note that there are some explanatory texts on larger screens.

plurals
  1. POHaving my else statement executed even if condition isn't met in jQuery
    primarykey
    data
    text
    <p>Well, my title can't be true, since a programming language can't make such a mistake, but it's happening. So we can safely say there is a problem in my logic.</p> <p>I have a piece of jQuery/JS code that checks for the number of characters <em>"to go"</em> just like here in SO's comment section before an AJAX call is made. So the code is thought like this:</p> <p><strong>If</strong> the number of characters in the input is less then N count the characters down (eg., 2 chars to go, 1 char to go...) <br /><strong>Else</strong> make an AJAX call and display the result on success.</p> <p>When you go to the form for the first time, this process goes smooth: </p> <ul> <li>You get the empty field</li> <li>Start typing</li> <li>The character indicator keeps decreasing</li> <li>We hit the "breakpoint" where it stops checking</li> <li>AJAX call is made</li> <li>We get a message displayed on the success callback</li> </ul> <p>But when I keep removing the characters with backspace (or delete) and I go again under the breakpoint (where the "N to go" must be seen) I get the "N to go" part displayed again, but the AJAX call goes through too even though it's wrapped in an <code>else</code>! This is pretty weird. The result looks like this:</p> <p><img src="https://i.imgur.com/PiYuqVK.png" alt="AJAX failure"></p> <p>Where the red text is the AJAX success callback and the yellow part is the character counter.</p> <p><strong>This is my jQuery:</strong></p> <pre><code>var delay = (function(){ var timer = 0; return function(callback, ms){ clearTimeout (timer); timer = setTimeout(callback, ms); }; })(); $('#reg_email').keyup(function(){ var min_password_chars = 6; var email_input = $('#reg_email').val(); var email_prev = ''; if (email_input.length &lt; min_password_chars) { $('.email-check').text(''); if ($('.email-check').children('.yellowish').length == 0) { $('.email-check').append('&lt;span class="yellowish"&gt;&lt;span class="counter"&gt;6&lt;/span&gt; to go&lt;/span&gt;'); } $('.email-check .counter').text(min_password_chars - email_input.length); } else { delay(function(){ $.ajax({ type: 'GET', url: 'ajax.php?action=check_email', data: { email: JSON.stringify(email_input) }, success: function(data){ var response = $.parseJSON(data); $('.email-check').append(response.message); email_prev += response.sent_email; } }); }, 1500); if (email_input != email_prev) { $('.email-check').text(''); } } }); </code></pre> <p><strong>And this is the HTML:</strong></p> <pre><code>&lt;div class="form-group"&gt; &lt;label for="rep_email"&gt;Repeat email&lt;/label&gt; &lt;input type="email" class="form-control" id="rep_email" name="email_rep" placeholder="Enter your email" required&gt; &lt;p class="email-check pull-right validate-note"&gt;&lt;span class="yellowish"&gt;&lt;span class="counter"&gt;6&lt;/span&gt; to go&lt;/span&gt;&lt;/p&gt; &lt;/div&gt; </code></pre> <p>What could I possibly be doing wrong? Where is my logic failing me?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload