1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Returning Values from a function

Discussion in 'jQuery' started by sarahk, Nov 28, 2015.

  1. #1
    I've got some javascript doing a bunch of stuff and in the middle of it I need to get a colour based on a value in my function and a call to the database. I've been messing around with my function getCoverageColourCode and neither version works the way I want.

    when I view the console I get
    What am I missing that my the variable colour in drawStuff isn't catching the returned value?

    function drawStuff(position){
        var colour = getCoverageColourCode(position);
        console.log('colour about to be applied: '+ colour);
           }
    
       
    function getCoverageColourCode(position){
    
                $.ajax({
                    url: "/data.php",
                    data: position.toJSON(),
                    async: false,  // shouldn't have this, I know
                    type: 'GET',
                    success: function(data, textStatus, jqXHR){
                        console.log('Success: '+data);
                        return data;
                    }
                });
        }
    
        function getCoverageColourCode1(position){
    
            $.when(
                $.ajax({
                    url: "/data.php",
                    data: position.toJSON(),
                    async: false,
                    type: 'GET',
                })
                .then(
                    function (response){
                        console.log('Success: '+response);
                        return response;
                    },
                    function (reasons) {
                        console.log('Fail: '+reasons);
                        return '#FF0000';
                    }   
                    )
                );
        }
    Code (markup):
     
    sarahk, Nov 28, 2015 IP
  2. sarahk

    sarahk iTamer Staff

    Messages:
    28,487
    Likes Received:
    4,455
    Best Answers:
    123
    Trophy Points:
    665
    #2
    Fixed it, had to have a change of strategy. Instead of getting the building blocks and putting it altogether I've changed it so that each building block initiates the call to the next building block and it works a treat.
     
    sarahk, Nov 28, 2015 IP