询问有没有这样一款android 定时器 timer

再弱弱的问一个问题,如何做一个计时器 - CNode技术社区
这家伙很懒,什么个性签名都没有留下。
做一个计时器,让时间在一个控件上显示出来。setInterval可以定时,只是如何定时的时候在控件上显示呢?不刷新页面用ajax可以吗?那又怎么实时改变控件内容呢?我只知道在render的时候通过加载.ejs等文件做到。刚开始看nodejs的菜鸟,请大家帮忙。最好有个实例代码我直接运行下就更好了。呵呵。先谢了哈
前台实现用 HTML + JAVASCRIPT
正巧有一个短信倒计时的例子给你:
function Timer(){
if(typeof Timer.instance ===“object”){
return Timer.
Timer.instance =
//private var
var _MAX_TIME = 0
,currentTime=0
,clock=null
//public function
this.initial = function(MAX_TIME){
_MAX_TIME=MAX_TIME;
this.start = function(){
currentTime=_MAX_TIME;
clearInterval(clock);
clock = setInterval(function(){
currentTime--;
updateContent();
if(currentTime&=0){
currentTime = _MAX_TIME;
clearInterval(clock);
this.stop = function(){
currentTime = _MAX_TIME;
clearInterval(clock);
//private function
function updateContent(){
//
console.log(currentTime);
使用方式:
var timer = new Timer();
timer.initial(60)//设定60秒倒计时
timer.start();//开始倒计时,可以控制台看
自己重写updateContent()方法或者做继承,你自己选吧
控制台下面的程序我也能实现,关键是想显示在一个页面上不知道怎么做。理论上可以在setInterval的函数中每次post,然后重新render这个页面,让那个控件重新显示传入的值。但是那样会不会性能很慢?有没有不刷新页面,而只修改单个控件内容的方法?多谢了!
求demo,一直不知道怎么做。我能想到的办法就是在setInterval的函数中,每次post一下,然后重新render页面。但是那样性能估计会很慢吧。应该有不重新render而能改变某个控件的办法吧。有人说用ajax,用了下,估计是用的方法不对,还是不行。
不就是显示当前时间么?
你要显示时间用new Date().getTime()的方式不行吗,写在updateContent()中,你的UI操作怎么做你自己的事了,$().html(“时间”)不行吗?
搜一下 javascript 显示当前时间,这根nodejs没关系
貌似明白了,哈哈
呵呵。当时有点糊涂,貌似控件的显示完全是前端的事情,和nodejs没什么关系。谢了哈
再问一下,在javascript 有document这个对象,在html中用document.conName.value就能改变控件的值x.现在我在nodejs中的ejs文件里面,运行,他说没有document这个对象。呵呵
木有事,我常有脑容量不足的时候…
再问一下,在javascript 有document这个对象,在html中用document.conName.value就能改变控件的值x.现在我在nodejs中的ejs文件里面运行,他说没有document这个对象。
你知道在ejs文件里面有没有类似的方法吗?
理论上ejs文件里&% %&之间的代码也应该是javascript,不知道为啥不行。
你没有明白这个过程,ejs最后要渲染为html文件,这个时候dom树才是建立的,也就是说你需要待你要操作的dom树建立了之后再进行操作。
啊,这样啊。我再补补基础知识。哈哈,多谢了。
没有对象的原因大概是你的js运行时,ejs还没有解析完毕,可以这样做:window.onload=function(){
document.getElementById(’’)
没事,共同进步
嘿嘿,再打扰下,我window.onload这个是直接写在ejs文件里面还是写在render那个ejs文件的js文件里。 之前我调用的那个函数是ejs文件&% %&中的setTimeout里调用的。
ejs就是个html模板,写在ejs里
js都写在&script&&/script&里
好的。多谢。
CNode 社区为国内最专业的 Node.js 开源技术社区,致力于 Node.js 的技术研究。
服务器赞助商为
,存储赞助商为
,由提供应用性能服务。
新手搭建 Node.js 服务器,推荐使用无需备案的多个EOS连接同一个数据库出现定时器表锁定的问题解决方案
问题描述:
&如果多个EOS连接同一个数据库,在server启动的时候,会出现EOS_QRTZ_LOCKS锁表的情况,导致Server启动不成功
解决方案:
&&&多个环境连通过库,容易出现定时器锁表的问题,因为定时器默认情况下是启动,可尝试如下操作: &module name="Schedule"&
&group name="Default"& &!-- IsSchedulerStart[optional,default to
"true"], whether to start timer when application starts up --&
&configValue key="IsSchedulerStart"&true&/configValue& &!--
DataSouceName[optional,default to "default"], datasouce name(must be the same as
DataSource's Group name) --& &!-- configValue
key="DataSouceName"&default&/configValue--& &/group&
&/module& 将颜色标记的true改为false(连同一个库的每个环境都需要改)
配置文件&路径
apps_config\default\config\user-config.xml
后方可回复
如果没有账号可以
一个帐号。
关注EAWorld公众号,加入技术微信群
每天都有新见解找个大咖做朋友
每周一节图文课时刻紧跟技术流
普元产品知识库
提供普元官方文档在线阅读以及超过2073个咨询故障和800+知识点。
普元产品知识库
提供普元官方文档在线阅读以及超过2073个咨询故障和800+知识点。
该话题的其他文章
该作者的其他文章
欢迎来到普元技术社区。
普元是国内领先的软件基础平台与解决方案提供商,主要面向大中型企业、政府机构及软件开发商提供SOA、大数据、云计算三大领域的软件基础平台及解决方案,用以满足上述组织信息化建设对关键技术的需求,帮助上述组织的业务在云计算和移动互联时代向数字化转型。普元系国家规划布局内重点软件企业,并是国际标准组织OASIS成员、SOA国际标准SCA/SDO的主要参与制定者、全国信标委SOA分技术委员会SOA与Web服务工作组副组长单位、全国信标委云计算工作组成员单位。找不到答案?
用Spring定时器写了个程序,查询数据库固定次数后,内存溢出
该程序是用Spring定时器执行的,定时用hibernate框架查询数据库,将查询到的结果写成文本文件。
启动定时器之后每次在生成了52个文本文件之后,就内存溢出了。
由于代码不多我直接贴代码和错误提示吧!
希望各位高手有时间帮忙看一下!
下面的是定时器执行的类和方法
public class BillInfoExporter {
private static String[] LOCATIONS = { "spring-hibernate.xml", "spring-minidao.xml" };
private Map&String, String&
protected final static Logger logger = Logger.getLogger("log4j.properties");
@SuppressWarnings("unused")
public boolean billInfoExport() {
ApplicationContext ctx =
BillingService billingService =
TextFileWriter textFileWriter =
File file =
List&Map& ioGoods =
List&Map& selfGoods =
if (ctx == null) {
ctx = new ClassPathXmlApplicationContext(LOCATIONS);
if (billingService == null) {
billingService = (BillingService) ctx.getBean("billingServiceImpl");
StringBuffer buff = new StringBuffer();
ioGoods = billingService.billingIOGoodsMessage();
BillExporterUtils.billExport(buff, ioGoods);
selfGoods = billingService.billingSelfGoodMessage();
BillExporterUtils.billExport(buff, selfGoods);
file = new File(buildFileName());
textFileWriter = new TextFileWriter(file, false);
textFileWriter.outPut(buff.toString());
} catch (Exception e) {
String resultLog = "未能成功从数据库中查询自用物资、备案清单进出单号信息,当前系统日期为:" + CalendarUtils.formatDate(new Date());
logger.error(resultLog, e);
} finally{
if(textFileWriter!=null){
textFileWriter.closeFile();
textFileWriter =
if (ioGoods != null) {
if (selfGoods != null) {
selfGoods =
System.gc();
} catch (Exception e) {
e.printStackTrace();
private String buildFileName(){
String fileName =
StringBuffer sBuffer = new StringBuffer();
Date currentDate = new Date();
fileName = config.get("outputPath");
if (StringUtils.isBlank(fileName)) {
fileName = System.getProperty("user.dir") + "/output";
System.out.println(fileName);
sBuffer.append(fileName).append("/");
sBuffer.append("SB_");
sBuffer.append(CalendarUtils.format(currentDate, "MM")).append('_');
sBuffer.append(currentDate.getTime());
sBuffer.append(".bi");
fileName = sBuffer.toString();
return fileN
public Map&String, String& getConfig() {
public void setConfig(Map&String, String& config) {
this.config =
下面是加载Spring定时器的主函数类
public class BillInfoScheduler {
public static void main(String[] args) {
String configFile =
if(args!=null && args.length&0){
configFile = args[0].trim();
configFile = "scheduler.xml";
execute(configFile);
} catch (Exception e) {
e.printStackTrace();
public static void execute(String configFile){
ApplicationContext ctx = new ClassPathXmlApplicationContext(configFile);
下面是定时器的配置文件
&?xml version="1.0" encoding="UTF-8"?&
&!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd"&
&!-- 第零步: 配置计费信息输出的路径 --&
&bean id="config" class="java.util.HashMap"&
&constructor-arg&
&entry key="outputPath" value="D:/work_dev/YanKe/Source/ECLink_packed/output" /&
&/constructor-arg&
&!-- 第一步: 配置好要定时调用的业务类 --&
&bean id="billInfoExporter" class="org.broadengate.eclink.schedule.BillInfoExporter"&
&property name="config" ref="config" /&
&!-- 第二步: 定义好具体要使用类的哪一个业务方法 --&
&bean id="printTimerJob"
class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean"&
&!-- 目标bean --&
&property name="targetObject" ref="billInfoExporter" /&
&!-- 要执行目标bean的哪一个业务方法 --&
&property name="targetMethod" value="billInfoExport" /&
&!-- 是否并发 --&
&property name="concurrent" value="false" /&
&!-- 第三步: 定义好调用模式: 如每隔2秒钟调用一次或每天的哪个时间调用一次等 --&
&bean id="printTimerTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean"&
&property name="jobDetail" ref="printTimerJob" /&
&property name="cronExpression" value="0/2 * * * * ?" /&
&!-- 每月最后一日的上午10:15触发 0 15 10 L * ? --&
&!-- 每天晚上23:00触发 0 0 23 * * ? --&
&!-- 启动定时器 --&
&!--第四步 把定义好的任务放到调度(Scheduler)工厂里面,注意这里的ref bean --&
&bean id="schedulerFactoryBean"
class="org.springframework.scheduling.quartz.SchedulerFactoryBean"&
&property name="applicationContextSchedulerContextKey" value="applicationContext" /&
&property name="triggers"&
&ref bean="printTimerTrigger" /&
&/property&
&!-- end --&
控制台报的错,是说我在执行定时任务的方法是时由于内存溢出而无法执行吗?
[org.quartz.core.JobRunShell]Job DEFAULT.printTimerJob threw an unhandled Exception:
org.springframework.scheduling.quartz.JobMethodInvocationFailedException: Invocation of method 'billInfoExport' on target class [class org.broadengate.eclink.schedule.BillInfoExporter] nested exception is java.lang.OutOfMemoryError: PermGen space
at org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean$MethodInvokingJob.executeInternal(MethodInvokingJobDetailFactoryBean.java:320)
at org.springframework.scheduling.quartz.QuartzJobBean.execute(QuartzJobBean.java:113)
at org.quartz.core.JobRunShell.run(JobRunShell.java:216)
at org.quartz.simpl.SimpleThreadPool$WorkerThread.run(SimpleThreadPool.java:549)
Caused by: java.lang.OutOfMemoryError: PermGen space
按投票排序
按时间排序
BillInfoExporter是定时器调用的类,billInfoExport()是定时器执行的方法。
原先,我在billInfoExport()方法中创建Spring的ApplicationContext对象,并且通过它获得我要使用的Service类对象。
这样一来,每次定时器调用该方法都去初始化Spring,创建很多Bean实例。于是就内存溢出了……
修改后的代码:
public class BillInfoExporter {
private static String[] LOCATIONS = { "spring-hibernate.xml",
"spring-minidao.xml" };
private Map&String, String&
protected final static Logger logger = Logger.getLogger("log4j.properties");
private static ApplicationContext ctx =
private static BillingService billingService =
if (ctx == null) {
ctx = new ClassPathXmlApplicationContext(LOCATIONS);
if (billingService == null) {
billingService = (BillingService) ctx.getBean("billingServiceImpl");
@SuppressWarnings("unused")
public boolean billInfoExport() {
TextFileWriter textFileWriter =
File file =
List&Map& ioGoods =
List&Map& selfGoods =
StringBuffer buff = new StringBuffer();
ioGoods = billingService.billingIOGoodsMessage();
BillExporterUtils.billExport(buff, ioGoods);
selfGoods = billingService.billingSelfGoodMessage();
BillExporterUtils.billExport(buff, selfGoods);
file = new File(buildFileName());
textFileWriter = new TextFileWriter(file, false);
textFileWriter.outPut(buff.toString());
} catch (Exception e) {
String resultLog = "未能成功从数据库中查询自用物资、备案清单进出单号信息,当前系统日期为:"
+ CalendarUtils.formatDate(new Date());
logger.error(resultLog, e);
} finally {
if (textFileWriter != null) {
textFileWriter.closeFile();
textFileWriter =
if (ioGoods != null) {
if (selfGoods != null) {
selfGoods =
System.gc();
} catch (Exception e) {
e.printStackTrace();
登录后可回答问题,请
一种高性能、高可靠、可平滑扩容的分布式内存数据库服务。
是解决用户结构化数据搜索需求的托管服务,支持数据结构、搜索排序、数据处理自由定制。 为您的网站或应用程序提供简单...
针对互联网服务器(包括非阿里云主机)在遭受大流量的DDoS攻击后导致服务不可用的情况下,推出的付费增值服务,用户...
为您提供简单高效、处理能力可弹性伸缩的计算服务,帮助您快速构建更稳定、安全的应用,提升运维效率,降低 IT 成本...主题 : 初学者,问个简单的问题啊,定时器调用的方法可以带参数吗
级别: 新手上路
可可豆: 131 CB
威望: 121 点
在线时间: 97(时)
发自: Web Page
初学者,问个简单的问题啊,定时器调用的方法可以带参数吗&&&
比如这样,模仿打飞机游戏,想添加不同的飞机,schedule(schedule_selector(Thrower::addNewFlyer), 1.0f);这个addNewFlyer方法可以带参数吗,如果不能,这个问题怎么解决?不能一个类型的飞机写一个方法吧
级别: 侠客
UID: 125820
可可豆: 535 CB
威望: 536 点
在线时间: 216(时)
发自: Web Page
回 楼主(twilightdream) 的帖子
schedule(schedule_selector(Thrower::newMethod), 1.0f);在newmethod里添加方法 addNewFlyer: 就可以带上参数了的吧
当你穿过一场风暴 请高昂你的头 不要害怕黑暗 在那风暴的尽头 是片金色的天空 和百灵那甜美的歌声 穿过风 穿过雨 你的梦想或许会破灭 但带着你心中的希望前行 你永远不会独行
级别: 新手上路
可可豆: 131 CB
威望: 121 点
在线时间: 97(时)
发自: Web Page
回 1楼(1024) 的帖子
我的意思是,比如要生成个大飞机我就addNewFlyer(1),小飞机addNewFlyer(2),类似这样,由于他们刷新间隔不一样,需要精确控制,不能用随机,这个有什么办法吗?能给个思路吗
级别: 侠客
UID: 125820
可可豆: 535 CB
威望: 536 点
在线时间: 216(时)
发自: Web Page
回 2楼(twilightdream) 的帖子
我的意思是schedule(schedule_selector(Thrower::newMethod), 1.0f);的newMethod中调用方法addNewFlyer(), 在这里设置参数 addNewFlyer(1)、addNewFlyer(2)随便你了嘛
当你穿过一场风暴 请高昂你的头 不要害怕黑暗 在那风暴的尽头 是片金色的天空 和百灵那甜美的歌声 穿过风 穿过雨 你的梦想或许会破灭 但带着你心中的希望前行 你永远不会独行
级别: 新手上路
可可豆: 131 CB
威望: 121 点
在线时间: 97(时)
发自: Web Page
回 3楼(1024) 的帖子
明白了,谢谢
级别: 版主
UID: 267887
可可豆: 634 CB
威望: 763 点
在线时间: 384(时)
发自: Web Page
用schedule定时调用runaction CCCallFunc不就可以带参数了
关注本帖(如果有新回复会站内信通知您)
发帖、回帖都会得到可观的积分奖励。
按"Ctrl+Enter"直接提交
关注CocoaChina
关注微信 每日推荐
扫一扫 关注CVP公众号
扫一扫 浏览移动版}

我要回帖

更多关于 定时器 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信