We help IT Professionals succeed at work.

Check out our new AWS podcast with Certified Expert, Phil Phillips! Listen to "How to Execute a Seamless AWS Migration" on EE or on your favorite podcast platform. Listen Now

x

2 references to same variable returning different values

mcunn
mcunn asked
on
Medium Priority
234 Views
Last Modified: 2012-05-11
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

Comment
Watch Question

CERTIFIED EXPERT
Expert of the Year 2008
Top Expert 2008
Commented:
Unlock this solution with a free trial preview.
(No credit card required)
Get Preview
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a free trial preview!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.