How have you found to deal with the inconsistencies of the Titanium timers?
For example, I am saving an image rendered from an imageView and have to wait for the image to show (and no, changing the image in an existing imageView will not fire the image load event). So I use a 2 second timer that will fire a function to finish the process. However, the timer will work the first time, but will not fire with any subsequent calls to the function.
Code-wise it is something like:
var windowHome = Titanium.UI.createWindow(skinStyles.windowHome); var profilePic = Titanium.UI.createImageView(skinStyles.profilePic); windowHome.add(profilePic); windowHome.open(); function choosePicture() { Titanium.Media.openPhotoGallery({ success: function (file) { savePicture(file); }, error: function () { cancelPicture(); }, cancel: function () { cancelPicture(); }, showControls: true, allowEditing: true, mediaTypes: [Titanium.Media.MEDIA_TYPE_PHOTO] }); } function cancelPicture() { //cleanup some garbage here } function continueSavePicture() { Titanium.API.info("New picture loaded"); var file = pPic.toImage(); pPic.url = imageStyles.defaultProfile; pPic.canScale = false; var tempFile = Titanium.Filesystem.getFile(Titanium.Filesystem.applicationDataDirectory, 'somename.png'); if (tempFile.exists() == true) { tempFile.deleteFile(); } Titanium.API.info("Writing file: " + tempFile.nativePath); tempFile.write(file.media); Titanium.API.info("Wrote file: " + tempFile.nativePath); pPic.url = tempFile.nativePath; cancelPicture(); } function savePicture(file) { var tempFile; if(file == null) { cancelPicture(); return; } var tempDir = Titanium.Filesystem.createTempDirectory(); if (tempDir.exists() == false) { tempDir.createDirectory(); } tempFile = Titanium.Filesystem.getFile(tempDir.nativePath, 'somename.png'); tempFile.write(file.media); //image scaling and transforms hiding here pPic.url = tempFile.nativePath; pPic.show(); //never fires a second time!!! setTimeout(continueSavePicture, 2000); } pPic.addEventListener('click', function(e){ choosePicture(); });
1 Answer
hmm, I think you may need to use setInterval and clearInterval:
"Timers
Titanium has built-in support for one-shot and repeating timers with two main functions: setTimeout and setInterval. setTimeout takes 2 arguments: function and timeout in milliseconds after which the function should be executed and returns a timer handle that can be used to cancel a pending timer with clearTimeout. setInterval takes 2 arguments: function and timeout in milliseconds for how often the function should be executed until cancelled and returns a timer handle that can be used to cancel a timer with clearInterval."
Your Answer
Think you can help? Login to answer this question!