Stop a Function


#1

I have a function that loads a bunch of thumbnails. When you click a button it loads a bunch of other thumbnails. If the previous thumbnails load slower, they will load where the current ones should be after the current ones load.

The solution I was thinking of was just killing the old function before starting the next one. I tried using

 function = null;
but then it won’t start up again.

I hope that made sense. Thanks.


#2

if you would like to stop a function, just end it using:

return;

However, it doesn’t sound like this is your problem. If you are using a periodic function (one that executes over several frames), you could use an onEnterFrame like so:

this.onEnterFrame = function() {

// do some stuff.

}

…and end the function using:

this.onEnterFrame = null;

Or consider making your function inside a movie clip. You can then destroy the movie clip to end the function, and create a new instance.

If you have events triggering from the loading of images, consider attaching a unique code to the movie clip that will hold the image. When the image loads, it can double-check to make sure that it has the same code as the holder. This will keep old images from loading in the place of new ones.

Hope something I said was useful. Good luck!


#3

Config:

startFunction = function() {}
startFunction_init = function() { //do something }
startFunction_stop = function() { startFunction = null; }
startFunction_start = function() { startFunction = startFunction_init(); }

To start:

startFunction_start();
startFunction();

To stop:

startFunction_stop();

[Wait, does that make sense? my brain hurts.]


#4

Guys, guys!

You can’t stop a function you can only write a function that can return early.

Every FlashPlayer to date has 1 thread of execution for your code. Loaders, the Sound API, video etc… they are on the other side of the sandbox wall, and the host threads those appropriately.

But you cannot intercept a running function. Not even with an event. If an event is triggered by a system callback (progress, socketData etc.) it queues until your code has run.

I’ve been asking for threads (even if they’re lime green) on feature request forms for the last few years. Until then, the only way to abort a function, is for that function to abort itself.

There might be room for a new compiler + language (you can get the Flex compiler sources if you want to hack at it) that can green thread the current ABC. I don’t have time to write it though I’d love to.


#5

Something like this.


function something() {
     // do something
     return;
}

Anything after return; will not run. You could write a few conditionals in that function and use the same function for what you are trying to achieve.


#6

I am an idiot. I fix my problem without having to stop any functions. I thought I was naming my clips uniquely so that having an old image loading into a new movie wouldn’t work. All my clips were named clipundefined_mc. The undefined should be a number. When it’s a number the problem goes away. Thanks!