<div id="remind"></div>
百度文字转语音接口
function audioPlay(text) { var zhText = text; zhText = encodeURI(zhText); var audio = "<audio autoplay=\"autoplay\">" + "<source src=\"http://tts.baidu.com/text2audio?lan=zh&ie=UTF-8&spd=4&text=" + zhText + "\" type=\"audio/mpeg\">" + "<embed height=\"0\" width=\"0\" src=\"http://tts.baidu.com/text2audio?text=" + zhText + "\">" + "</audio>"; document.getElementById("remind").innerHTML = audio; }
时间函数
function getTime() { var myDate = new Date(); var myYear = myDate.getFullYear(); //获取完整的年份(4位,1970-????) var myMonth = myDate.getMonth() + 1; //获取当前月份(0-11,0代表1月) var myToday = myDate.getDate(); //获取当前日(1-31) var myDay = myDate.getDay(); //获取当前星期X(0-6,0代表星期天) var myHour = myDate.getHours(); //获取当前小时数(0-23) var myMinute = myDate.getMinutes(); //获取当前分钟数(0-59) var mySecond = myDate.getSeconds(); //获取当前秒数(0-59) var week = ['星期日', '星期一', '星期二', '星期三', '星期四', '星期五', '星期六']; var nowTime = myYear + '-' + fillZero(myMonth) + '-' + fillZero(myToday) + ' ' + fillZero(myHour) + ':' + fillZero(myMinute) + ':' + fillZero(mySecond) + " " + week[myDay]; return nowTime; } function fillZero(str) { var realNum; if (str < 10) { realNum = '0' + str; } else { realNum = str; } return realNum; }
封装自动刷新时间自动播报语音
function getAllData() { getTime(); var info = '现在时间是;' + getTime(); audioPlay(info); } //定时刷新数据; var timerAll; getAllData() clearInterval(timerAll); timerAll = setInterval(getAllData, 10 * 1000);
Done!