2

I am trying to create a temporary image url for a local image and send it to Google to do a Search by Image. I don't want the image url to be permanent so I want to delete it right after I use it. I have the code below:

// Gets a URL that can be used to do a search by image through Google.
function getImageURL() {
    var xml = new XMLHttpRequest();
    xml.onreadystatechange = function () {
        if (xml.readyState == 4 && xml.status == 200) {
            deleteImageURL(); // asynchronous call to server to delete the URL
            window.location.href = 
                "https://www.google.com/searchbyimage?site=search&sa=X&image_url=" 
                + xml.responseText; // the image url
        }
    }
    xml.open("GET", "REST_ENDPOINT", true);
    xml.send();
}

The function above calls the server, and when it finishes, will delete the url and redirect the page. The function "deleteImageURL()" is another ajax call done asynchronously. Currently, this loads the google page fine as the image URL is not done deleting the url by the time that the redirect happens.

My question is this: Will deleteImageURL() finish deleting the image URL even after the page redirects or will it stop (and thus, never delete the URL)?

EDIT: So I was thinking about what you guys were saying about race conditions and tried the following code instead:

// Gets a URL that can be used to do a search by image through Google.
function getImageURL() {
    var xml = new XMLHttpRequest();
    xml.onreadystatechange = function () {
        if (xml.readyState == 4 && xml.status == 200) {
            deleteImageURL(xml.responseText);
        }
    }
    xml.open("GET", "REST_ENDPOINT"
        + id + "/public_url", true);
    xml.send();
}

// Deletes the url for the image.
function deleteImageURL(imageURL) {
    var xml = new XMLHttpRequest();
    xml.open("GET", "REST_ENDPOINT_FOR_DELETE", true);
    xml.send();
    window.location.href = 
            "https://www.google.com/searchbyimage?site=search&sa=X&image_url="
            + imageURL;
}

This code works every time that I run it. I think that there still may be a race condition, but it seems to be working fine so far.

Thanks again.

Jon Downs
  • 83
  • 1
  • 6
  • 2
    It depends. sometimes it might be fast enough, other times it might not be. I would expect it to not be fast enough on most modern machines. – Kevin B Jul 16 '15 at 16:43
  • Your redirect should wait until the Ajax call comes back. – epascarello Jul 16 '15 at 16:44
  • You should have a backend "garbage collector". A script you automatically execute every hour or so, that deletes URLs older than X minutes, for example. Don't rely on the client to do that. Keep it server-side – blex Jul 16 '15 at 16:46
  • depends on how/when the browser starts loading the new url, and when it shuts down the page containing your ajax code, and how soon the server notices that the ajax connection has been terminated. e.g. it's a total race condition. – Marc B Jul 16 '15 at 16:46
  • possible duplicate of [Should I wait for ajax to complete to redirect a page?](http://stackoverflow.com/questions/17735747/should-i-wait-for-ajax-to-complete-to-redirect-a-page) – Ted Nyberg Jul 16 '15 at 16:49

3 Answers3

1

If deleteImageURL(); contains an async call you should do the redirect when the call is completed. Your code will work when the call is synchronious. We don't see the source of deleteImageURL(); and can be more concrete, but you should do the same thing as you've done for getImageURL().

Reflective
  • 3,854
  • 1
  • 13
  • 25
  • The request will be made before the next statement (window.location.href), so there should be no risk of the server request being ignored because of the following redirect? Or am I misunderstanding the question? – Ted Nyberg Jul 16 '15 at 16:49
  • the guy specifies that deleteImageURL() contains another async call ... so this call have to be completed, and then `oncomplete` to make the redirect, because otherwise the redirect will be executed immediatelly and the async call won't be peroformed ... it is time depending ...sometimes this call maight be executed – Reflective Jul 16 '15 at 16:53
1

The "deleteImageURL()" will finish deleting the image URL even after the page redirects.. Refer : Should I wait for ajax to complete to redirect a page?

Community
  • 1
  • 1
Archer
  • 34
  • 4
1

The server won't stop processing the request (initiated by deleteImageUrl), but you will not be able to handle a callback if the current page unloads in the browser before the operation is completed.

Ted Nyberg
  • 7,001
  • 7
  • 41
  • 72