有没有一种方法可以在一个给定的日期/时间使用动作脚本3.0来执行任务(函数)?我的意思是像schedule(myFunction:Function, dateTime:Date)
。谢谢使用动作脚本3.0的Cron作业
1
A
回答
2
本身不是本身。
在另一个答案中概述的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);
}
}
用途:
Scheduler.scheduleAction(myDateToRunTheFunction, myFunctionToRun, howOftenToCheckTime, functionParameter1, functionParameter2, functionParameter2);
2
这里是一个办法做到这一点,它需要在日期运行的功能,并发现了长有是如何运行的功能,然后调用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);
}
相关问题
- 1. 使用CRON作业执行PHP脚本
- 2. 使用php脚本安装cron作业
- 3. 使用Cron作业执行PHP脚本?
- 4. 使用cron作业执行PHP脚本
- 5. Python脚本vs Cron作业
- 6. Bash脚本Cron作业
- 7. Cron作业和php脚本
- 8. 动态cron作业?
- 9. shell脚本不从cron作业执行
- 10. Symfony2从cron作业执行脚本
- 11. cron作业有关R脚本未能
- 12. Cron作业,执行php脚本
- 13. Cron作业和php脚本问题
- 14. Cron作业未执行Perl脚本
- 15. Cron作业PHP脚本不会运行
- 16. 在cron作业中运行脚本
- 17. shell脚本wget在用作cron作业时不工作
- 18. 使用cron作业的pyspark自动化
- 19. 使用Laravel的Cron作业?
- 20. Cron作业调用bash脚本和python脚本
- 21. 使用表达式引擎脚本的Cron作业
- 22. 使用Cron作业启动Akeeba备份
- 23. 设置每45天的cron作业来执行我的php脚本使用cron?
- 24. 如何使用cron作业
- 25. json_decode在php脚本中不工作作为cron作业
- 26. Linux服务器上的自动脚本(不是cron作业)
- 27. 如何使用cpanel cron作业执行.sh脚本
- 28. 脚本运行一次一个月,但不使用cron作业
- 29. 需要通过cron作业使用cpanel运行python脚本
- 30. 使用php脚本和ajax请求控制cron作业
我喜欢这个实施“计划的'上课,我会的尝试一下!谢谢@LondonDrugs –
它是如何工作的?这样做的好处是,你可以暂停所有预定的事情(比如说,如果你有不想打扰的模式框),取消某些内容,并且始终可以访问队列中的内容。 – BadFeelingAboutThis
感谢您的帮助,但最后我用了一个简单的实现使用Timer类专门为我想解决的问题的建议..虽然很好的贡献 –