6.17周六隨寫
阿新 • • 發佈:2017-06-18
return iss 定時器 tin ++ txt tex () hang
class F { constructor() { // 獲取新聞消息 this.newsCb = null; // 用戶的回調 this.aNews = []; // 接口數據 this.getNewsTimer = null; // 獲取新聞消息定時器 this.intermissionShowNewsTimer = null; // 輪播新聞消息定時器 this.aNewsIndex = 0; //新聞索引 this.option = { getNewstime: 3, // 單位h intermissionShowNewsTime: 3000, //單位 ms, num: 5, // 取幾條數據 url: ‘https://news.xiaohulu.com/api.php?mod=js&bid=80‘ }; } /** * [getNewsInfo description] * @param {[Function]} newsCb [回調, err, res2個參數, err是錯誤信息, res是結果] * @param {[Object]} option [description] * /*f.getNewsInfo(function(err, res) { console.log(res); }, { num: 5, // 取幾條新聞輪播 getNewstime: 3, // 單位h intermissionShowNewsTime: 1000, //單位 ms url: ‘https://news.xiaohulu.com/api.php?mod=js&bid=80‘ }); setTimeout(()=>{ f.stop(); }, 10000)*/ getNewsInfo(newsCb, option) { if (this._isFunction(newsCb)) this.newsCb = newsCb; this.option = Object.assign(this.option, option); let self = this, url = this.option.url; let p = fetch(url).then((res)=> { return res.text(); }).then((res)=> { self._getInfoFromStr(res); }).catch((err)=> { self.newsCb(err); }); return this; } stop() { clearInterval(this.getNewsTimer); clearInterval(this.intermissionShowNewsTimer); this.getNewsTimer = null; // 獲取新聞消息定時器 this.intermissionShowNewsTimer = null; // 輪播新聞消息定時器 } _isFunction(fn) { return Object.prototype.toString.call(fn) === ‘[object Function]‘; } _getInfoFromStr(txt) { let patt1 = /<a([^>]*?)href=[‘"]([^‘"]*?)[‘"]([^>]*?)>(.*?)<\/a>/ig; let res = txt.match(patt1); let hrefArr = this._getHrefFromA(res); let TitleArr = this._getContFromTxt(txt); let result = []; hrefArr.forEach(function(item, index) { let obj = {}; obj.href = item, obj.title = TitleArr[index]; result.push(obj); }); this.aNews = result; // 獲取數據後就開啟3小時再次獲取數據 this._startUpGetNews(); this._startShowNews(); // 先搞起數據 ++ self.aNewsIndex; self.newsCb && self.newsCb(null, self._changeData()); } // 返回一個title數組 _getContFromTxt(txt) { let patt1 = /(?:[^_]+(?=\.html))|[^>]+(?=<\/a>)/g; let res = txt.match(patt1); return res; } // 返回一個href數組 _getHrefFromA(arr) { let result = []; result = arr.map(function(a, index) { a = a || "<a href=‘https://news.xiaohulu.com/portal.php?mod=view&aid=589‘ title=‘主播進階篇-敢想敢做,做一個“敢”的主播‘ target=‘_blank‘>主播進階篇-敢想敢做,做一個“敢”的主播</a>"; let href = ""; let hStart = "<a href=", hEnd = " title=", startIndex = a.indexOf(hStart), entIndex = a.indexOf(hEnd); if (startIndex !== -1 && entIndex != -1) { href = a.substring(hStart.length + 1, entIndex - 1); }; return href; }); return result; } // 開啟3小時獲取一次新聞消息 _startUpGetNews() { clearInterval(this.getNewsTimer); let h = this.option.getNewstime; let self = this; this.getNewsTimer = setInterval(()=>{ self.getInfo(‘‘, ‘‘); }, h*60*60*1000); } // 開啟每幾秒輪播一下新聞 _startShowNews() { let self = this; clearInterval(this.intermissionShowNewsTimer); this.intermissionShowNewsTimer = setInterval(function() { ++ self.aNewsIndex; self.newsCb && self.newsCb(null, self._changeData()); }, this.option.intermissionShowNewsTime); } // 換數據 _changeData() { // let length = this.aNews.length - 1; let length = this.option.num - 1; if (this.aNewsIndex > length) { this.aNewsIndex = 0; }; return this.aNews[this.aNewsIndex]; } } let f = new F(); f.getNewsInfo(function(err, res) { console.log(res); }, { getNewstime: 3, // 單位h num: 2, intermissionShowNewsTime: 1000, //單位 ms url: ‘https://news.xiaohulu.com/api.php?mod=js&bid=80‘ }); setTimeout(()=>{ f.stop(); }, 10000)
6.17周六隨寫