Loops 如何在Actionscript中为超过15秒的进程添加时间延迟?
因此,我有以下脚本来获取数组的所有组合: '''Loops 如何在Actionscript中为超过15秒的进程添加时间延迟?,loops,actionscript-3,time,while-loop,actionscript,Loops,Actionscript 3,Time,While Loop,Actionscript,因此,我有以下脚本来获取数组的所有组合: ''' var值=新数组(40) 对于(变量i=0;i 0&&a[i]==n-r+i){ 我--; } result.push(a.slice())//将数组打印为空格分隔的数字的伪代码 计数++; a[i]++; //将每个外部元件重置为上一个元件+1 而(i
var值=新数组(40)
对于(变量i=0;i 0&&a[i]==n-r+i){
我--;
}
result.push(a.slice())//将数组打印为空格分隔的数字的伪代码
计数++;
a[i]++;
//将每个外部元件重置为上一个元件+1
而(i
'''
运行上面的脚本将使我:
错误:错误#1502:脚本的执行时间超过了默认的15秒超时时间
如何每过14秒添加一次时间延迟,以便运行脚本?因此,14秒后,程序将等待50毫秒,然后继续
感谢您的帮助。该错误与您的脚本需要延时无关,问题在于您的while循环使脚本在15秒以上没有响应,从而触发脚本超时错误。操作脚本仅允许15秒的时间执行脚本 您的第一个while循环看起来有问题,我不清楚[0]的值如何改变以结束循环。在循环中添加一个中断或确保条件更改以允许循环结束,您应该解决您的问题。您还可以考虑将continue语句添加到嵌入的while循环中,前提是它们在找到不饱和值后只运行一次 就个人而言,因为您使用的是ActionScript,所以我建议使用对象和侦听器进行值更改,而不是迭代数组检查更改 您还可以为while循环添加一个手动超时,但需要包含逻辑,以便它能够从中断的地方恢复
//Set timer to 14 seconds
timeout = getTimer() + 14000;
while(true && timeout > getTimer()){
trace("No Error");
}
该错误与脚本需要时间延迟无关,问题在于while循环使脚本失去响应超过15秒,从而触发脚本超时错误。操作脚本仅允许15秒的时间执行脚本 您的第一个while循环看起来有问题,我不清楚[0]的值如何改变以结束循环。在循环中添加一个中断或确保条件更改以允许循环结束,您应该解决您的问题。您还可以考虑将continue语句添加到嵌入的while循环中,前提是它们在找到不饱和值后只运行一次 就个人而言,因为您使用的是ActionScript,所以我建议使用对象和侦听器进行值更改,而不是迭代数组检查更改 您还可以为while循环添加一个手动超时,但需要包含逻辑,以便它能够从中断的地方恢复
//Set timer to 14 seconds
timeout = getTimer() + 14000;
while(true && timeout > getTimer()){
trace("No Error");
}
因此,有一个简单的(很好,基本上如此)工作示例,说明如何将繁重的计算部分从主线程中分离出来,以便主线程(它还处理UI和外部事件,如用户输入)能够顺利运行,同时能够读取后台繁重计算的进度和结果。它也是以单个类的形式出现的,这可能会有点混乱(直到您理解它是如何工作的),但仍然很容易处理和修改
尽管后台AVM遵循相同的执行流程(代码执行>图形渲染>代码执行>图形渲染>等等),但没有要渲染的图形,因此无需限制代码执行时间。因此,工作者线程不受15秒限制的约束,这在某种程度上解决了问题
package
{
import flash.events.Event;
import flash.display.Sprite;
import flash.utils.ByteArray;
import flash.concurrent.Mutex;
import flash.system.Worker;
import flash.system.WorkerDomain;
public class MultiThreading extends Sprite
{
// These variables are needed by both the main and
// subservient threads and will actually point to
// the very same object instances, though from
// the different sides of this application.
private var B:ByteArray;
private var W:Worker;
private var M:Mutex;
// Constructor method.
public function MultiThreading()
{
super();
// This property is 'true' for the main thread
// and 'false' for any Worker instance created.
if (Worker.current.isPrimordial)
{
prepareProgress();
prepareThread();
startMain();
}
else
{
startWorker();
}
}
// *** THE MAIN THREAD *** //
private var P:Sprite;
private var F:Sprite;
// Prepares the progress bar graphics.
private function prepareProgress():void
{
F = new Sprite;
P = new Sprite;
P.graphics.beginFill(0x0000FF);
P.graphics.drawRect(0, 0, 100, 10);
P.graphics.endFill();
P.scaleX = 0;
F.graphics.lineStyle(0, 0x000000);
F.graphics.drawRect(0, 0, 100, 10);
F.x = 10;
F.y = 10;
P.x = 10;
P.y = 10;
addChild(P);
addChild(F);
}
// Prepares the subservient thread and shares
// the ByteArray (the way to pass messages)
// and the Mutex (the way to access the shared
// resources in a multi-thread environment
// without stepping on each others' toes).
private function prepareThread():void
{
M = new Mutex;
B = new ByteArray;
B.shareable = true;
B.writeObject(incomingMessage);
W = WorkerDomain.current.createWorker(loaderInfo.bytes);
W.setSharedProperty("message", B);
W.setSharedProperty("lock", M);
}
// Starts listening to what the background thread has to say
// and also starts the background thread itself.
private function startMain():void
{
addEventListener(Event.ENTER_FRAME, onFrame);
W.start();
}
private var incomingMessage:Object = {ready:0, total:100};
private function onFrame(e:Event):void
{
// This method runs only 20-25 times a second.
// We need to set a lock on the Mutex in order
// to read the shared data without any risks
// of colliding with the thread writing the
// same data at the same moment of time.
M.lock();
B.position = 0;
incomingMessage = B.readObject();
M.unlock();
// Display the current data.
P.scaleX = incomingMessage.ready / incomingMessage.total;
P.alpha = 1 - 0.5 * P.scaleX;
// Kill the thread if it signalled it is done calculating.
if (incomingMessage.terminate)
{
removeEventListener(Event.ENTER_FRAME, onFrame);
W.terminate();
B.clear();
B = null;
M = null;
W = null;
}
}
// *** THE BACKGROUND WORKER PART *** //
// I will use the same W, M and B variables to refer
// the same Worker, Mutex and ByteArray respectively,
// but you must keep in mind that this part of the code
// runs on a different virtual machine, so it is the
// different class instance thus its fields are not
// the same quite as well.
// Initialization.
private function startWorker():void
{
W = Worker.current;
M = W.getSharedProperty("lock");
B = W.getSharedProperty("message");
// Before starting the heavy calculations loop
// we need to release the main thread which is
// presently on W.start() instruction. I tried
// without it and it gives a huuuge lag before
// actually proceeding to intended work.
addEventListener(Event.ENTER_FRAME, onWorking);
}
private function onWorking(e:Event):void
{
removeEventListener(Event.ENTER_FRAME, onWorking);
var aMax:int = 10000000;
// Very very long loop which might run
// over the course of several seconds.
for (var i:int = 0; i < aMax; i++)
{
// This subservient thread does not actually need to
// write its status every single loop, so lets don't
// explicitly lock the shared resources for they
// might be in use by the main thread.
if (M.tryLock())
{
B.position = 0;
B.writeObject({ready:i, total:aMax});
M.unlock();
}
}
// Let's notify the main thread that
// the calculations are finally done.
M.lock();
B.position = 0;
B.writeObject({ready:i, total:aMax, terminate:true});
M.unlock();
// Release the used variables and prepare to be terminated.
M = null;
B = null;
W = null;
}
}
}
包
{
导入flash.events.Event;
导入flash.display.Sprite;
导入flash.utils.ByteArray;
导入flash.concurrent.Mutex;
导入flash.system.Worker;
导入flash.system.WorkerDomain;
公共类多线程扩展了Sprite
{
//这些变量是主变量和主变量都需要的
//从属线程,并将实际指向
//同样的对象实例,虽然来自
//此应用程序的不同方面。
私人风险值B:ByteArray;
私人变量W:工人;
私有var M:Mutex;
//构造函数方法。
公共函数多线程()
{
超级();
//此属性对于主线程为“true”
//对于创建的任何工作实例,为“false”。
if(Worker.current.isPrimordial)
{
prepareProgress();
prepareThread();
startMain();
}
其他的
{
startWorker();
}
}
//***主线程***//
私有变量P:Sprite;
私有变量F:Sprite;
//准备进度条图形。
私有函数prepareProgress():void
{
F=新精灵;
P=新雪碧;
P.graphics.Beginll(0x0000FF);
P.graphics.drawRect(0,0,100,10);
P.graphics.endFill();
P.scaleX=0;
F.图形线样式(0x000000);
F.图形drawRect(0,0,100,10);
F.x=10;
F.y=10;
P.x=10;
P.y=10;
addChild(P);
addChild(F);
}
//准备从属线程并共享
//ByteArray(传递消息的方式)
//和互斥(访问共享数据的方式)
//多线程环境中的资源
//不踩对方的脚)。
私有函数prepareThread():void
{
M=新互斥体;
B=新ByteArray;
B.可共享=真;
B.写入对象(包含消息);
W=WorkerDomain.current.createWorker(loaderInfo.bytes);
W.setSharedProperty(“消息”,B);
W.setSharedProperty(“锁定”,M);