Kris Coppieters
|
Hi John!
First the simple solution - which might or might not work for you depending on the context of what you're doing:
[code] function drawBoxes(total) { var progBar = _item.createProgressBar("Test Bar", 0, total, true); var i = 0; var cancelled = false; while (i < total && ! cancelled) { var t = _page.rectangles.add(); t.fillColor = _doc.swatches[0]; var rx = Math.random()*_page_width; var ry = Math.random()*_page_height; var newBounds = [ry, rx, ry+Math.random()*5, rx+Math.random()*5]; t.geometricBounds = newBounds; cancelled = t.setProgress(i); i++; } } [/code]
It is better to use the proper range in your progress bar (i.e. 0-total) instead of using 0-100 and then transform your progress to a value between 0-100.
For cancel handling also need to check the return value of t.setProgress(i) - it will normally return 'false' but if the user clicks 'Cancel' it will return 'true'. My example loop terminates as soon as the user clicks cancel.
Now the more detailed stuff. Progress bars and event handlers are intricately linked - if you create a progress bar it will only disappear when the event handler you're in terminates. Normally, that is when the user causes some handler to fire (selecting, modifying, selecting menu item,...) - the idea is that the progress bar comes up and disappears when the handler finishes.
So, the best way is to not relate progress bars to functions (as your example is doing) but rather to 'user actions' - show bar at beginning of user action (read - event handler), and it will auto-disappear at the end.
However, if you want to show and hide progress bars on-the-fly, there is a little trick: wrap your function into an event handler and the bar will disappear when the handler ends.
Try this:
[code] _item.javaScript = "drawBoxes(100);" _item.eventFilter = "drawBoxesEvent"; _item.handleScriptEvent("drawBoxesEvent");
function drawBoxes(total) { var progBar = _item.createProgressBar("Test Bar", 0, total, true); var i = 0; var cancelled = false; while (i < total && ! cancelled) { var t = _page.rectangles.add(); t.fillColor = _doc.swatches[0]; var rx = Math.random()*_page_width; var ry = Math.random()*_page_height; var newBounds = [ry, rx, ry+Math.random()*5, rx+Math.random()*5]; t.geometricBounds = newBounds; cancelled = t.setProgress(i); i++; } } [/code]
I used '_item' (which I assume is some page item) and temporarily assigned it a simple, one-line JavaScript:
drawBoxes(100);
Then I gave it an event code in its event filter (I randomly named it 'drawBoxesEvent'), and finally I sent the event to _item. So, I replaced the single line
drawBoxes(100);
with three lines:
_item.javaScript = "drawBoxes(100);" _item.eventFilter = "drawBoxesEvent"; _item.handleScriptEvent("drawBoxesEvent");
The end result is that the drawBoxes call is wrapped [i]inside[/i] an event, so when the drawBoxes(100) is done, the progress bar will disappear.
|