java后台或js前台每天定时任务

需要每天更新一次页面。获取到每天的近七天。。java后台怎么写。
或者前台js

这个需求放在前端实现,肯定是有问题的,建议放在后端实现

后端实现,有很多选择,首推是spring的quartz
参考:spring的quartz使用案例

<meta http-equiv="refresh" content="20">

20指每隔20秒刷新页面

JAVA使用定时器,JS触发这个任务不合理
定时器可以自己实现(最简单的就是减法判断)也可以用现成的,有独立的也有Spring自身继承的
关于定时器到底怎么用,望题主加深自己的思考

定时任务都交给后台写,前端在这一块负责的一般都是,短时间的轮询或者存储较长的时间的客户端信息。

使用注解@Scheduled

import org.springframework.scheduling.annotation.Scheduled;

import org.springframework.stereotype.Component;

@Component(“taskJob”)

public class TaskJob {

@Scheduled(cron = "0 0 3 * * ?")

public void job1() {

System.out.println(“任务进行中。。。”);

}

}

然后再Spring的XML文件中添加如下配置

CRON表达式 含义
"* * * * * ?" 每秒执行一次
"0/3 * * * * ?" 每3秒执行一次
"0 0 12 * * ?" 每天中午十二点触发
"0 15 10 ? * *" 每天早上10:15触发
"0 15 10 * * ?" 每天早上10:15触发
"0 15 10 * * ? *" 每天早上10:15触发
"0 15 10 * * ? 2005" 2005年的每天早上10:15触发

按照需求改一下就好

> Spring的XML文件中添加如下配置补充 “<”task:annotation-driven scheduler="qbScheduler" mode="proxy"/>

  1. 在Spring的配置文件ApplicationContext.xml,首先添加命名空间 xmlns:task="http://www.springframework.org/schema/task" http://www.springframework.org/schema/task
    http://www.springframework.org/schema/task/spring-task-3.1.xsd
    1. 最后是我们的task任务扫描注解 task:annotation-driven/
      1. spring扫描位置 context:annotation-config/

        4.写自己的定时任务 @Component //import org.springframework.stereotype.Component;
        public class MyTestServiceImpl implements IMyTestService {
        @Scheduled(cron="0/5 * * * * ? ") //每5秒执行一次
        public void myTest() {
        System.out.println("进入测试");
        }
        }

后台主动发出请求,,去更新浏览器(可以做推送,然后更新)

你这个需求(定时更新 ),,我感觉这样做也行,,,,js获取当前时间,,跟你定时比一下,,比上了就更新(js貌似可以使用多线程),,
这种比法,没有推送那种好,但是实现比较简单,可以隔一分钟,检测一次。

js获取当前时间及以后的一周时间,将当前时间放在

的第一个,一周时间就用当前时间+1循环显示,显示7天,这样每天页面都会更新
//获取下一周时间
function nextWeek(){
var dateStr = "";
var index = 0;
for(var i=0;i<7;i++){
index++;
var lastTime = $("#WeekPriceTemplate7").find("li:eq(1)").attr("predate");
$("#btnPreWeek").removeClass("pre_preWeek_gray").addClass("pre_preWeek");
var datetime = getDay(lastTime,i+1,0);
var weektime = getDay(lastTime, +(i+1),1)+' '+getweek(datetime);
if(datetime==$comParaters.txtDeptureDate.val()){
dateStr += queryStr(datetime, weektime,"week-price-tp current",index);
}else{
dateStr += queryStr(datetime, weektime,"week-price-con",index);
}

}
$("#WeekPriceContainer").html(dateStr);
}

http://blog.csdn.net/xuanzhangran/article/details/76070141

楼主的这个定时任务是干什么的? 查数据库的? 还是就是页面显示时间的?

页面显示时间就用js取一下值就好,如果页面或是界面会一直显示 ,再做个定时增加时间就好。
如果是查数据库数据的,就浪费了,逻辑结构也不能这么整!!!看你做哪个,可以细说!