Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Oct 2013
    Posts
    1
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Making a URL call again with $.get

    Hi everyone!

    I have a question regarding the following Javascript code:

    Code:
    var url = "http://apple.accuweather.com/adcbin/apple/Apple_find_city.asp?location="+escape(obj.extraLocCity)+","+obj.extraLocCountryCode;
    $.get(url, function(data) {
    var us =  $(data).find('CityList').attr('us')*1;
    var intl = $(data).find('CityList').attr('intl')*1;
    var extra_cities = $(data).find('CityList').attr('extra_cities')*1;
    var exist = intl + us + extra_cities;
    If "exist" is "0", I would like to basically stop the script, go up and make the URL call again but using a different "url" variable. Specifically, using "obj.extraLocNeighborhood" in place of "obj.extraLocCity".

    Any help would be greatly appreciated! Happy to provide more information if need be. Thank you in advance!

  • #2
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,436
    Thanks
    13
    Thanked 360 Times in 356 Posts
    it depends a little on how often that repetition can occur.

    some idea out-of-the-blue:

    PHP Code:
    var url = {
        
    urls : [/* array of urls */],
        
    index 0,
        
    next : function() {
            if (
    url.index >= url.urls.length) {
                throw new 
    Error("No more URLs to try.");
            }
            return 
    url.urls[url.index++];
        },
        
    repeat : function _r(cb) {
            $.
    get(urls.next(), function(data) {
                
    // ...
                
    if (!exist) {
                    return 
    url.repeat(cb);
                }
                
    cb(processedData);
            });
        }
    };

    // usage
    try {
        
    url.repeat(function(data) {
            
    // ...
        
    });
    }
    catch (
    e) {
        $(
    '#error').text(e.message);

    Last edited by Dormilich; 10-28-2013 at 12:02 PM.
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer

  • Users who have thanked Dormilich for this post:

    Marty McFly (11-03-2013)


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •