0

I have a JavaScript snippet:

function verifyFrnds(){
    var boxes=$(".matchFrnds:checked").length;
    //alert(boxes); its value is 50 when you do alert
    var call=1;
    $(".matchFrnds").each(function(index){

        if($(this).is(':checked')){
            call++;
            var sendData= $(this).val();

            $.post('SOME PHP Page',{sendData:sendData},function(data){      
                //window.location.reload();
            });

            //alert(call); value is 1
            // 1 >=50 should be false but all the time the condition gets true
            if(call >= boxes)
            {
                window.location.reload();
            }
        }
    });     
}

The question is self explanatory. The conditions gets true even when it is not. Not sure if it is due to that it is not treating them as numbers and strings may be, but all the time the condition gets true.

JJJ
  • 32,902
  • 20
  • 89
  • 102
  • have you check the boxes values and call variables values? just add alert before alert(cal); and alert(boxes); – Indranil.Bharambe Nov 30 '13 at 07:43
  • You don't touch the unchecked items in your loop, why have the separate test anyway? Just loop over `$(".matchFrnds:checked").each()` – Quentin Nov 30 '13 at 07:43
  • `alert(boxes)`? Please keep us informed. –  Nov 30 '13 at 07:43
  • 1
    1) [Don't use `alert()` for debugging](http://blogs.msdn.com/b/cdndevs/archive/2011/05/26/console-log-say-goodbye-to-javascript-alerts-for-debugging.aspx) and 2) [there's nothing immediately wrong with what you've posted](http://jsfiddle.net/YYVh3/). Based on the commented out reload line in the Ajax call I'd guess there's something there that makes you think the later condition is triggered when it actually is not, but hard to say without seeing a complete example. – JJJ Nov 30 '13 at 07:48
  • Being an "optimization freak" I wouldn't do 50 separate XHR requests but one request for all matching condition (checked) and based on the rules on the server send the needed callback to the client. – qdev Nov 30 '13 at 07:52
  • @Terror.Blade yes and I mentioned in the commpents –  Nov 30 '13 at 08:05
  • @Juhana anything related to typecasting may be java is considering them as string –  Nov 30 '13 at 08:06
  • but have you checked the value of boxes . that was my concern – Indranil.Bharambe Nov 30 '13 at 08:06
  • @Terror.Blade yes and it was 50 –  Nov 30 '13 at 08:06
  • @wared 50 was the alert –  Nov 30 '13 at 08:07
  • Why do you think the condition is true, then? – JJJ Nov 30 '13 at 08:07
  • 1
    `alert(call+' '+(typeof call)+' '+boxes+' '+(typeof boxes)+' '+(call>=boxes));`, paste the output in a comment. –  Nov 30 '13 at 08:10
  • @Juhana may be something related to strings, –  Nov 30 '13 at 08:11
  • @user1765876 No, I mean why do you say that the line `if(call >= boxes)` is "always true"? How have you confirmed that? Do you have an alert inside the if as well? Because what you've posted here, there's only a reload command there; you can't say that the condition is "always true" just because the page is reloaded at some point. – JJJ Nov 30 '13 at 08:13
  • 1
    @wared it is 2 32 false –  Nov 30 '13 at 08:19
  • So, this is the expected result right? –  Nov 30 '13 at 08:23
  • @wared Yes but the page reloads –  Nov 30 '13 at 08:25
  • @Juhana You got it right, " the condition is "always true" just because the page is reloaded at some point" well noted –  Nov 30 '13 at 08:26
  • @user1765876 If the condition is always `true`, how did you alert such a result? –  Nov 30 '13 at 08:30
  • Nice that I was downvoted for that ! – qdev Nov 30 '13 at 08:31
  • @wared If the condition is true or false the window.location.reload() gets executed –  Nov 30 '13 at 08:34
  • @qdev didn't get your point here –  Nov 30 '13 at 08:35
  • @user1765876 Sure, so, how `2 32 false` is possible? Ok, forget it... :) –  Nov 30 '13 at 08:39
  • @wared if it is false then why the page reloads regardless of the false condition? –  Nov 30 '13 at 08:41
  • 1
    Without a live example we will argue here over this or that and maybe the issue is somewhere else. – qdev Nov 30 '13 at 08:46
  • It reloads because *at some point* the condition is true. The logic is way off --- `boxes` is the number of checked checkboxes, and you loop through all checked checkboxes, so of course the condition is true for the last checkbox (or actually already for the next to last because the count starts from 2), regardless of how many checkboxes are checked. – JJJ Nov 30 '13 at 09:10
  • @Juhana I loop through all check boxes but it gets true before it loop through all, because you can see the AJAX request I made when the checkbox is checked..nad it insert somevalue in DB for each checkbox in the DB, so if the page reloads then it should insert value after processing for all checked boxes / –  Nov 30 '13 at 13:05
  • Ajax requests are *asynchronous*. The reload happens before all/any of them have time to complete, even if the reload is triggered by the last loop iteration. See e.g. [this](http://stackoverflow.com/questions/3952387/jquery-wait-until-all-ajax-calls-finish-then-continue) (tl;dr: put the counter in the Ajax callback.) – JJJ Nov 30 '13 at 15:00
  • @Juhana still not getting it, when, then are defered in jquery –  Nov 30 '13 at 16:37

3 Answers3

0

I'm missing some context but try this:

function verifyFrnds() { 

    var boxes = $(".matchFrnds:checked").length;

    $(".matchFrnds:checked").each(function(index) {

        var sendData= $(this).val();
        $.post('SOME PHP Page',{sendData:sendData},function(data){      
            //window.location.reload();
        });

        if(index >= boxes-1) {
            window.location.reload();
            return false;
        }

    });

}
-1

use below code

if(parseInt(call) >= parseInt(boxes))
            {
                window.location.reload();
            }
Indranil.Bharambe
  • 1,462
  • 3
  • 14
  • 25
  • I am trying in a minute –  Nov 30 '13 at 08:18
  • Sorry, but this doesn't make any sense at all. First of all, the variables are initialized as integers so converting them from integer to integer does nothing. Secondly, even if they would change into strings by some code that hasn't been shown to us, JavaScript implicitly converts strings to integers for comparison, i.e. `"1" >= "50" === false`. – JJJ Nov 30 '13 at 08:19
  • 1
    as you see in code op defined call=1; and after that he is doing call++; still user saying that call value is 1 so that means call is not geting added if that converted to int then call value must be 2 but as it is not happening i thought user might give shot to convert into int forcefully. – Indranil.Bharambe Nov 30 '13 at 08:22
  • @Terror.Blade If the condition is true or false the window.location.reload() gets executed –  Nov 30 '13 at 08:36
  • It will work without "=" in if condition like this if(parseInt(call) >parseInt(boxes)) – Ravinder Singh Bhanwar Mar 28 '14 at 13:34
-1

Doesn't window.location.reload() from your post callback resets your checkboxes thus none being checked ?

1 will always be greater than 0

Of course that this depends on how the form is initialized in HTML, but we don't know this from your example.

Maybe if you present to us a more detailed explanation of what you have and what you want to do, we can present you a better solution.

qdev
  • 1,371
  • 1
  • 15
  • 18
  • If the page is reloaded the script execution is stopped as well. Even if the checkboxes are reset the value of variable `boxes` doesn't automatically change. – JJJ Nov 30 '13 at 08:15
  • @Juhana - we also don't know what triggers the JS execution either, not explained. Could be on document ready. And you're wrong. If the page reloads, and no checkbox is checked and the function is called again, it will be reinitialized with 0. – qdev Nov 30 '13 at 08:17
  • All the variables are inside the same function. Execution time doesn't matter. – JJJ Nov 30 '13 at 08:18
  • If the function is called again, the `if` is never encountered because for every checkbox `if($(this).is(':checked')` is false. – JJJ Nov 30 '13 at 08:19
  • the boxes would be anyway zero as stated, but you're correct, it will not get to the check anymore – qdev Nov 30 '13 at 08:21