Link to home
Start Free TrialLog in
Avatar of mcunn
mcunn

asked on

2 references to same variable returning different values

Ok I have a function that's driving my nutty.

I get 2 different values for a variable (recipecount) on sequential calls

Note the area on line  108

 
alert(recipecount);  //Returns 0
            alert(recipecount);  //Returns 5

Open in new window


Here is the complete function

 
function doSearch() {
        try {

            // Set URL We Want To Fetch
            var url = 'http://www.eatchicken.com/chickenhookups/chickenhookupsresults.aspx?find=';
            // Set Initial Recipe Count To 0
            var recipecount = 0;
            // Random Recipe Number
            var randomseed;
            // Records Requested
            var recordsrequested = 3;
            // Image for Popup
            var hookupimage;
            // What to Search For
            var query = $("#find").val();
            // What Category
            var cat = $('input[name=drpPrep]:checked').val();


            //Alert if No Category Selected
            if (cat == "" || cat == null) {
                alert('Be sure to check a chicken type!');
                return false;
            }


            $.getJSON(url + query + '&catname=' + cat + '&groupid=5' + '&callback=?', function (json) {
                var randomseed;

                // Treat recipe as an array even though it will always return 1 result
                $.each(json[0].Recipe, function (i, recipe) {
                    // RecipeCount will be used as ceiling number for randomize
                    recipecount++
                    // End Each
                     });


                //Pick a Random Recipe From Results
                randomseed = Math.floor(Math.random() * recipecount);

                //Update Thickbox Popup

                //Map Choices To Image 
                switch (cat) {
                    case 'Baked':
                        hookupimage = "http://www.chickenhookups.com/wp-content/themes/chickenhookups_theme/images/Baked.png";
                        break;
                    case '|Broil':
                        hookupimage = "http://www.chickenhookups.com/wp-content/themes/chickenhookups_theme/images/Broil.png";
                        break;
                    case 'Fried':
                        hookupimage = "http://www.chickenhookups.com/wp-content/themes/chickenhookups_theme/images/Fried.png";
                        break;
                    case 'Grill':
                        hookupimage = "http://www.chickenhookups.com/wp-content/themes/chickenhookups_theme/images/Grill.png";
                        break;
                    case 'Marinade':
                        hookupimage = "http://www.chickenhookups.com/wp-content/themes/chickenhookups_theme/images/Marinade.png";
                        break;
                    default:
                        hookupimage = "http://www.chickenhookups.com/wp-content/themes/chickenhookups_theme/images/Baked.png";
                        //End Switch
                }


                //Set Image   
                $("#match_result_image").attr("src", hookupimage);

                // Set Url
                $("a#match_result_url").attr("href", "http://www.eatchicken.com/recipedetail.aspx?id=" + json[0].Recipe[randomseed].ID);

                // Set Recipe Title
                $("#match_result_title").html(json[0].Recipe[randomseed].RecipeName);

                // Facebook Link
                $("#st_facebook").attr("st-url", "http://www.eatchicken.com/recipedetail.aspx?id=" + json[0].Recipe[randomseed].ID);

                // Twitter Link
                $("#st_twitter").attr("st-url", "http://www.eatchicken.com/recipedetail.aspx?id=" + json[0].Recipe[randomseed].ID);

                // Email Link
                $("#st_email").attr("st-url", "http://www.eatchicken.com/recipedetail.aspx?id=" + json[0].Recipe[randomseed].ID);

                // Dynamically load share this, we have to back load this to fire after we have changed the fields.
                var switchTo5x = true;

                $.getScript('http://w.sharethis.com/button/buttons.js', function () {
                    stLight.options({
                        publisher: '6ddf0f4e-d8c8-4485-abcb-2ddbb25c3ae8'
                    });
                });


                //Close Json
            });


            //************************************************
            // PROBLEM IS HERE
            //************************************************


            alert(recipecount);  //Returns 0
            alert(recipecount);  //Returns 5
            

            if (recipecount < 1) {
               alert('There are so many great chicken hookups in the world but you did not find one. Maybe you should be a bit less picky with your keyword.');

               return false;

            }


            //Pop Up Thickbox
            fireThickBox();
            //Exit Function
            return true;

       } 
        catch (ex) {

            alert('There was an error:' + ex);
            return false;
            }
    
    }

Open in new window


I'm assuming this is because of the inline JSONP function but I can't figure out why.

Any ideas?

Thanks

Michael

ASKER CERTIFIED SOLUTION
Avatar of hielo
hielo
Flag of Wallis and Futuna image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial