FYI - I was looking at the rest of your code and found something that is probably not working the way you think it will. I have a question at the bottom of this message regarding a couple of pieces (shown below). I hope you don’t mind unsolicited feedback for something you may still be working on, but I wanted you to be aware of a potential problem you may experience with the current code.
var id = data.weather.id,
Earlier in the code you have:
My question is if another id besides the ones in backgroundId comes back from the API call, why do you think your current code with the use of sort and indexOf is going to be able to tell you the index of an image that you do not currently have in your backgroundImg array?