Skip welcome & menu and move to editor
Welcome to JS Bin
Load cached copy from
 
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>JS Bin</title>
</head>
<body>
<h3>W3C工作组当前发稿周期</h3>
<div id="result"></div>
<h5 id="today"></div>
</body>
</html>
 
// const epoch = 15371136e5
// 因冯通退出,2018-11-07将起始“纪元”重置为Date.parse('2018-10-17')
const epoch = Date.parse('2018-11-14')
const pofday = 864e5
const writers = ['何文力', '刘宇晨', '黄小璐', '李松峰', '刘博文', '刘观宇', '安佳', '高峰']
const today = getToday()
// 测试用:将以后任意一天作为“今天”
// const today = new Date('2018-11-15')
let length = window.location.search.split('?length=')[1]
length = length ? length : 2
// 查询几个周期
const result = getCurrentTurn(length)
const d = document.getElementById('result')
d.innerHTML = fmtResult(result)
const t = document.getElementById('today')
// t.innerHTML = genDate(today)
/*
 * 自动根据当前日期,显示当前所在轮次所有人的发稿时间
 */
function getCurrentTurn(turns = 1) {
  let time = findStartPoint()
  const pubDays = []
  while (pubDays.length < writers.length * turns) {
    if ('13'.indexOf(new Date(time).getDay()) > -1)
      pubDays.push(time)
    time += pofday
  }
  const result = pubDays.map((d, i) => {
    const index = i % writers.length
    const writer = writers[index]
    const date = genDate(d)
    return [date, writer]
  })
  return result
}
function findStartPoint() {
  let time = epoch
  let i = 0
  const turnStarts = []
  // 最初轮的起点时间
  while (time < today) {
    // 如果是发稿日,则i++
    if ('13'.indexOf(new Date(time).getDay()) > -1) {
      i++
      // 如果i为初始发稿日,则记录该发稿日
      if (i === 1) turnStarts.push(time)
    }
    // 如果i等于作者数,则一轮结束,i重置
    if (i === writers.length) i = 0
    time += pofday
  }
  // 返回最接近今天的最后一个发稿日
  return turnStarts.pop()
}
/*
 * 生成器函数
 */
function* gen(turns, all) {
  if (typeof turns !== 'number' && typeof all !== 'number') {
    return '无效参数。调用示例:g = gen(1)或g = gen(2,\'all\')'
  }
  const pubDays = []
  let time = epoch
  while (pubDays.length < turns * writers.length) {
    if ('13'.indexOf(new Date(time).getDay()) > -1)
      pubDays.push(time)
    time += pofday
  }
  const result = pubDays.map((d, i) => {
    const index = i % writers.length
    const writer = writers[index]
    const date = genDate(d)
    return [date, writer]
  })
  // 如果不是查看全部轮次,则仅返回第turns轮的结果
  if (all !== 'all') {
    const startIndex = (turns - 1) * writers.length
    const currentIteration = result.splice(startIndex, writers.length)
    yield currentIteration
    return
  }
  // 如果是查看全部轮次,则依次显示全部轮次
  while (turns > 0) {
    const startIndex = (turns - 1) * writers.length
    const currentIteration = result.splice(startIndex, writers.length)
    yield currentIteration
    turns--
  }
}
function getToday() {
  const temp = new Date()
  return new Date(temp.getFullYear() + '-' + (temp.getMonth() + 1) + '-' + (temp.getDate()+2) ).valueOf()
}
function genDate(d) {
  const temp = new Date(d)
  return temp.getFullYear() + '-' + padding((temp.getMonth() + 1)) + '-' + padding(temp.getDate())
}
function padding(v) {
  v = String(v)
  return v.length === 1 ? '0' + v : v
}
function fmtResult(ret) {
  const lis = ret.map(item => {
    return `<li><span class="date">${item[0]}</span> ${item[1]}</li>`
  }).join('')
  return `<ul>${lis}</ul>`
}
// const d = document.getElementById('result')
// const g = gen(2,'all')
// const t1 = g.next().value
// const t2 = g.next().value
// const t3 = g.next().value
// d.innerHTML = fmtResult(t1)
Output

This bin was created anonymously and its free preview time has expired (learn why). — Get a free unrestricted account

Dismiss x
public
Bin info
anonymouspro
0viewers