Created
July 7, 2010 16:45
-
-
Save dasher/466936 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var updatePeriod = 5 * 60 * 1000; //mins * seconds * 1000 | |
var locationUpdateEnabled = false; | |
// This gets fired when the webView has loaded. | |
// Rather than using the generic event of the webview | |
// - it's more reliable to create your own and have the page itself trigger when it's ready | |
// - This allows your code to initialise things properly before asking for update | |
Ti.App..addEventListener('pageReadyForUpdates', function(e) { | |
// This will only get fired once and when the page itself is ready to start receiving update | |
locationUpdateEnabled = true; | |
updateLocation(); | |
}); | |
function updateLocation() { | |
// Store the value in a placeholder - so we don't lose any prev data | |
var cityFound = getCity(); | |
if (cityFound.error) { | |
// Deal with the error | |
// Here we can stop future checks for updates | |
locationUpdateEnabled = false; | |
// Note the interval timer is still running - if you want to stop this also then | |
// clearInterval(triggerUpdate); | |
} else { | |
// We have valid data | |
city = cityFound; | |
data = new Date(); | |
// fire the event into the webview - | |
// but let's send it to an event listener that's specific to receiving updates to the location | |
// You'll need an eventListener in the page to pick this up | |
Ti.App.fireEvent('pageUpdateLocation', {data:data, city:city}); | |
} | |
} | |
var triggerUpdate = setInterval(function(e){ | |
if (locationUpdateEnabled) { | |
updateLocation(); | |
} | |
}, updatePeriod); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment