显示本周的消息

时间:2018-04-04 05:12:08

标签: javascript jquery arrays sorting message

我正在尝试实现一个小脚本,按顺序显示消息形成一个数组,每个星期都是另一个数组。我开始了,但我卡住了。 代码是

let daySun = ['msg1','msg2','msg3','msg4'];
let dayMon = ['msg1','msg2','msg3','msg4'];
let dayTue = ['msg1','msg2','msg3','msg4'];
let dayWed = ['msg1','msg2','msg3','msg4'];
let dayThu = ['msg1','msg2','msg3','msg4'];
let dayFri = ['msg1','msg2','msg3','msg4'];
let daySat = ['msg1','msg2','msg3','msg4'];

let dayWeek = new Date().getDay();
//How to get

setInterval(function(){
// 1. to compare name of the day with above arrays
// 2. if today is Sun, then messages from daySun array to be displayed in  order
// 3. and so on for every day of the week
// 4. I want to add fadeIn or fadeOut, or a animate class from  animate.min.css

},1000);

//html for messages is <p id="day-msg"></p>

2 个答案:

答案 0 :(得分:1)

创建几天的消息对象:

let messages = {
    sun: ['msg1','msg2','msg3','msg4'],
    mon: ['msg1','msg2','msg3','msg4'],
    tue: ['msg1','msg2','msg3','msg4'],
    wed: ['msg1','msg2','msg3','msg4'],
    thu: ['msg1','msg2','msg3','msg4'],
    fri: ['msg1','msg2','msg3','msg4'],
    sat: ['msg1','msg2','msg3','msg4']
}

获取当天的姓名:

let dayName = new Date().toString().split(' ')[0].toLowerCase();

查找当天的消息:

let currentMessages = messages[dayName];

在显示消息时将消息索引存储在数组中,最初为0

let currentIndex = 0;

setInterval(function(){
    console.log(currentMessages[currentIndex]);
    currentIndex++;
    // below code to loop your messages, if you want to close messages clear interval.
    if(currentIndex >= currentMessages.length) {
        currentIndex = 0;
    }
},1000);

答案 1 :(得分:0)

完成参考js fiidle

HTML

let day ={"sun":['msg1','msg2','msg3','msg4'],"mon":['msg1','msg2','msg3','msg4'],"Tue":['msg1','msg2','msg3','msg4'],"wed":['msg1','msg2','msg3','msg4'],"thu":['msg1','msg2','msg3','msg4'],"fri":['msg1','msg2','msg3','msg4'],"sat":['msg1','msg2','msg3','msg4']};


let dayName = new Date().toString().split(' ')[0].toLowerCase()

currentIndex = 0;
setInterval(function(){

document.getElementById("day-msg").innerHTML = day[dayName][currentIndex++];
 if(currentIndex >= day[dayName].length){
   currentIndex= 0;
  } 
},1000);

java脚本

donut