Actionscript 3 带有操作脚本3.0的Cron作业
有没有办法通过Action Script 3.0计划任务函数在给定日期/时间执行?我的意思是类似schedulemyFunction:Function,dateTime:Date的东西。谢谢不是天生的 另一个答案中列出的SetTimeout可能是最简单的赌注。但是如果你有很多你想要安排的事情,这里有一个类可以管理它:它可以修改为使用setTimeout而不是计时器,但是我更喜欢计时器Actionscript 3 带有操作脚本3.0的Cron作业,actionscript-3,flash,actionscript,flash-cs5,Actionscript 3,Flash,Actionscript,Flash Cs5,有没有办法通过Action Script 3.0计划任务函数在给定日期/时间执行?我的意思是类似schedulemyFunction:Function,dateTime:Date的东西。谢谢不是天生的 另一个答案中列出的SetTimeout可能是最简单的赌注。但是如果你有很多你想要安排的事情,这里有一个类可以管理它:它可以修改为使用setTimeout而不是计时器,但是我更喜欢计时器 public class Scheduler { public var time:Date; p
public class Scheduler {
public var time:Date;
public var action:Function;
public var parameters:Array;
private var checkInterval:Number = NaN;
public function get interval():Number { return checkInterval; };
public function set interval(val:Number):void {
checkInterval = val;
if (schedules && (mainTimer && mainTimer.delay > val)) {
mainTimer.delay = val;
}
}
public function Scheduler(time_:Date, action_:Function, interval_:Number = NaN, parameters_:Array = null):void {
time = time_;
action = action_;
checkInterval = interval_;
parameters = parameters_;
}
//static stuff
private static var mainTimer:Timer;
public static function stop():void {
if (mainTimer) {
mainTimer.stop();
}
}
public static function start():void {
if (mainTimer && !mainTimer.running) {
mainTimer.start();
}
}
public static function get curInterval():Number { return (mainTimer) ? mainTimer.delay : 0; };
private static var scheduleList:Vector.<Scheduler>;
public static function get schedules():Vector.<Scheduler> { return scheduleList; };
/**
* Schedules a function to run at a certain time (with the margin of the interval)
* @param time - what time to run this passed action
* @param action - a function to call between the time passing, and the next interval
* @param interval - how often to check if the time has come, default is 1 second
* @param ... rest - parameters to pass to the action method
* @return
*/
public static function scheduleAction(time:Date, action:Function, interval:Number = NaN, ... rest):Scheduler {
var s:Scheduler = new Scheduler(time, action, interval, rest);
//if already old
if (time.time < new Date().time) {
action.apply(null, rest);
return s;
}
if (!scheduleList) {
scheduleList = new Vector.<Scheduler>();
}
scheduleList.push(s);
if (!mainTimer) {
mainTimer = new Timer(1000);
mainTimer.addEventListener(TimerEvent.TIMER, timerTick);
mainTimer.start();
}
if (!isNaN(interval) && interval < mainTimer.delay) {
mainTimer.delay = interval;
}
return s;
}
private static function timerTick(e:TimerEvent):void {
var tmpDate:Date = new Date();
for (var i:int = scheduleList.length-1; i >= 0;i--){
if (tmpDate.time >= scheduleList[i].time.time) {
scheduleList[i].action.apply(null, scheduleList[i].parameters);
removeSchedule(i);
}
}
checkTimerNeeded();
}
private static function checkTimerNeeded():void {
if (scheduleList && scheduleList.length < 1) {
mainTimer.stop();
mainTimer.removeEventListener(TimerEvent.TIMER, timerTick);
mainTimer = null;
scheduleList = null;
}
}
private static function removeSchedule(index:int):void {
scheduleList.splice(index, 1);
checkTimerNeeded();
}
/**
* Cancels a scheduled item
* @param s - the item to cancel
* @return returns true if the item was scheduled, false if the item wasn't scheduled
*/
public static function cancelSchedule(s:Scheduler):Boolean {
if (scheduleList) {
var index:int = scheduleList.indexOf(s);
if (index > 0) {
removeSchedule(index);
return true;
}
}
return false;
}
public static function status():void {
trace("isRunning:", (mainTimer) ? mainTimer.running : null);
trace("List Length:", scheduleList ? scheduleList.length : null);
}
}
不是天生的
另一个答案中列出的SetTimeout可能是最简单的赌注。但是如果你有很多你想要安排的事情,这里有一个类可以管理它:它可以修改为使用setTimeout而不是计时器,但是我更喜欢计时器
public class Scheduler {
public var time:Date;
public var action:Function;
public var parameters:Array;
private var checkInterval:Number = NaN;
public function get interval():Number { return checkInterval; };
public function set interval(val:Number):void {
checkInterval = val;
if (schedules && (mainTimer && mainTimer.delay > val)) {
mainTimer.delay = val;
}
}
public function Scheduler(time_:Date, action_:Function, interval_:Number = NaN, parameters_:Array = null):void {
time = time_;
action = action_;
checkInterval = interval_;
parameters = parameters_;
}
//static stuff
private static var mainTimer:Timer;
public static function stop():void {
if (mainTimer) {
mainTimer.stop();
}
}
public static function start():void {
if (mainTimer && !mainTimer.running) {
mainTimer.start();
}
}
public static function get curInterval():Number { return (mainTimer) ? mainTimer.delay : 0; };
private static var scheduleList:Vector.<Scheduler>;
public static function get schedules():Vector.<Scheduler> { return scheduleList; };
/**
* Schedules a function to run at a certain time (with the margin of the interval)
* @param time - what time to run this passed action
* @param action - a function to call between the time passing, and the next interval
* @param interval - how often to check if the time has come, default is 1 second
* @param ... rest - parameters to pass to the action method
* @return
*/
public static function scheduleAction(time:Date, action:Function, interval:Number = NaN, ... rest):Scheduler {
var s:Scheduler = new Scheduler(time, action, interval, rest);
//if already old
if (time.time < new Date().time) {
action.apply(null, rest);
return s;
}
if (!scheduleList) {
scheduleList = new Vector.<Scheduler>();
}
scheduleList.push(s);
if (!mainTimer) {
mainTimer = new Timer(1000);
mainTimer.addEventListener(TimerEvent.TIMER, timerTick);
mainTimer.start();
}
if (!isNaN(interval) && interval < mainTimer.delay) {
mainTimer.delay = interval;
}
return s;
}
private static function timerTick(e:TimerEvent):void {
var tmpDate:Date = new Date();
for (var i:int = scheduleList.length-1; i >= 0;i--){
if (tmpDate.time >= scheduleList[i].time.time) {
scheduleList[i].action.apply(null, scheduleList[i].parameters);
removeSchedule(i);
}
}
checkTimerNeeded();
}
private static function checkTimerNeeded():void {
if (scheduleList && scheduleList.length < 1) {
mainTimer.stop();
mainTimer.removeEventListener(TimerEvent.TIMER, timerTick);
mainTimer = null;
scheduleList = null;
}
}
private static function removeSchedule(index:int):void {
scheduleList.splice(index, 1);
checkTimerNeeded();
}
/**
* Cancels a scheduled item
* @param s - the item to cancel
* @return returns true if the item was scheduled, false if the item wasn't scheduled
*/
public static function cancelSchedule(s:Scheduler):Boolean {
if (scheduleList) {
var index:int = scheduleList.indexOf(s);
if (index > 0) {
removeSchedule(index);
return true;
}
}
return false;
}
public static function status():void {
trace("isRunning:", (mainTimer) ? mainTimer.running : null);
trace("List Length:", scheduleList ? scheduleList.length : null);
}
}
下面是一种方法,它需要输入运行函数的日期,并找出运行函数的时间,然后调用setTimeout
var date:Date = new Date(2012,9,13);
schedule(myFunction,date);
private function myFunction():void
{
trace("Schedule function run");
}
private function schedule(func:Function,date:Date):void
{
var now:Date = new Date();
if (date.time < now.time)
return;
var timetorun:Number = date.time - now.time;
setTimeout(func, timetorun);
}
下面是一种方法,它需要输入运行函数的日期,并找出运行函数的时间,然后调用setTimeout
var date:Date = new Date(2012,9,13);
schedule(myFunction,date);
private function myFunction():void
{
trace("Schedule function run");
}
private function schedule(func:Function,date:Date):void
{
var now:Date = new Date();
if (date.time < now.time)
return;
var timetorun:Number = date.time - now.time;
setTimeout(func, timetorun);
}
我喜欢“Scheduler”类的这个实现,我要试试!谢谢,伦敦药展成功了吗?这样做的好处是,你可以暂停所有计划的事情,比如说,如果你有模态框,你不想被打扰,取消一些东西,并且总是可以访问队列中的内容。谢谢你的帮助,但最后我在一个更简单的实现中使用了你的建议,使用Timer类专门解决了我想要解决的问题。。很好的贡献,虽然我喜欢“调度器”类的这个实现,但我会试试!谢谢,伦敦药展成功了吗?这样做的好处是,你可以暂停所有计划的事情,比如说,如果你有模态框,你不想被打扰,取消一些东西,并且总是可以访问队列中的内容。谢谢你的帮助,但最后我在一个更简单的实现中使用了你的建议,使用Timer类专门解决了我想要解决的问题。。不过贡献不错