亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定

curSeconds與nextSeconds相等,數組添加失敗

請問一下老師,setIntval的update方法中,獲取下次刷新時間nextSeconds應該會比curSeconds稍微快一些,但是現在curSeconds與nextSeconds相等,導致添加小球進數組的方法進不去,只有倒計時效果,小球掉落效果偶爾會出現,請問老師應該怎么修改代碼呢。

var width = 1920;
var height = 768;
var R = 6;
var r = 8;
var DAY_MARGIN_LEFT = 10;
var DAY_MARGIN_TOP = 10;

var MARGIN_LEFT = 400;
var MARGIN_TOP = 200;


const endTime = new Date(2018, 7, 18, 0, 0, 0);
var curShowTimeSeconds = 0;
var littleLemon = [];
const colors = ["rgba(255,241,0,1)", "rgba(50,205,50,1)", "rgba(0,255,0,1)"];




window.onload = function () {
? ?var canvas = document.getElementById("canvas");
? ?var context = canvas.getContext("2d");
? ?canvas.width = width;
? ?canvas.height = height;
? ?curShowTimeSeconds = getCurrentShowTimeSeconds();


? ?setInterval(
? ? ? ?function () {
? ? ? ? ? ?render(context);
? ? ? ? ? ?update();
? ? ? ?}
? ? ? ?,
? ? ? ?24
? ?);
}

function getCurrentShowTimeSeconds() {
? ?var curTime = new Date();
? ?var ret = endTime.getTime() - curTime.getTime();
? ?ret = Math.round(ret / 1000);

? ?return ret >= 0 ? ret : 0;
}


function update() {
? ?var nextShowTimeSeconds = getCurrentShowTimeSeconds();

? ?var nextDay = parseInt(nextShowTimeSeconds / 86400);
? ?var nextHours = parseInt((nextShowTimeSeconds - nextDay * 86400) / 3600);
? ?var nextMinutes = parseInt((nextShowTimeSeconds - (nextDay * 86400) - (nextHours * 3600)) / 60);
? ?var nextSeconds = nextShowTimeSeconds % 60;

? ?var curDay = parseInt(curShowTimeSeconds / 86400);
? ?var curHours = parseInt((curShowTimeSeconds - curDay * 86400) / 3600);
? ?var curMinutes = parseInt((curShowTimeSeconds - (curDay * 86400) - (curHours * 3600)) / 60);
? ?var curSeconds = curShowTimeSeconds % 60;

? ?console.log("curSeconds:" + curSeconds);
? ?console.log("nextSeconds:" + nextSeconds);


? ?if (nextSeconds !== curSeconds) {

? ? ? ?//"時"產生變化時添加的小球
? ? ? ?if (parseInt(curHours / 10) !== parseInt(nextHours / 10)) {
? ? ? ? ? ?addLemon(MARGIN_LEFT, MARGIN_TOP, parseInt(curHours / 10));
? ? ? ?}
? ? ? ?if (parseInt(curHours % 10) !== parseInt(nextHours % 10)) {
? ? ? ? ? ?addLemon(MARGIN_LEFT + 16 * (r + 1), MARGIN_TOP, parseInt(curHours % 10));
? ? ? ?}

? ? ? ?//"分"產生變化時添加的小球
? ? ? ?if (parseInt(curMinutes / 10) !== parseInt(nextMinutes / 10)) {
? ? ? ? ? ?addLemon(MARGIN_LEFT + 46 * (r + 1), MARGIN_TOP, parseInt(curMinutes / 10));
? ? ? ?}
? ? ? ?if (parseInt(curMinutes % 10) !==parseInt(nextMinutes % 10)) {
? ? ? ? ? ?addLemon(MARGIN_LEFT + 62 * (r + 1), MARGIN_TOP, parseInt(curMinutes % 10));
? ? ? ?}

? ? ? ?//"秒"產生變化時生成的小球
? ? ? ?if (parseInt(curSeconds / 10) !== parseInt(nextSeconds / 10)) {
? ? ? ? ? ?addLemon(MARGIN_LEFT + 92 * (r + 1), MARGIN_TOP, parseInt(curSeconds / 10));
? ? ? ?}
? ? ? ?if (parseInt(curSeconds % 10) !== parseInt(nextSeconds % 10)) {
? ? ? ? ? ?addLemon(MARGIN_LEFT + 108 * (r + 1), MARGIN_TOP, parseInt(curSeconds % 10));
? ? ? ?}
? ? ? ?curShowTimeSeconds = nextShowTimeSeconds;
? ?}

? ?updateLemon();
? ?console.log(littleLemon.length + "hello");
}


// 添加變化的數字到littleLemon數組中
function addLemon(x, y, num) {
? ?console.log("添加數組:");

? ?for (var i = 0; i < digit[num].length; i++)
? ? ? ?for (var j = 0; j < digit[num][i].length; j++)
? ? ? ? ? ?if (digit[num][i][j] == 1) {
? ? ? ? ? ? ? ?var aLemon = {
? ? ? ? ? ? ? ? ? ?x: x + j * 2 * (r + 1) + (r + 1),
? ? ? ? ? ? ? ? ? ?y: y + i * 2 * (r + 1) + (r + 1),
? ? ? ? ? ? ? ? ? ?g: 1.5 + Math.random(),
? ? ? ? ? ? ? ? ? ?vx: Math.pow(1, Math.ceil(Math.random() * 1000)) * 20,
? ? ? ? ? ? ? ? ? ?vy: -3,
? ? ? ? ? ? ? ? ? ?color: colors[Math.floor(Math.random() * colors.length)]
? ? ? ? ? ? ? ?};
? ? ? ? ? ? ? ?littleLemon.push(aLemon);
? ? ? ? ? ?}


}


//將數字依次排列
function render(ctx) {
? ?ctx.clearRect(0, 0, width, height);

? ?curShowTimeSeconds = getCurrentShowTimeSeconds();
? ?var day = parseInt(curShowTimeSeconds / 86400);
? ?var hours = parseInt((curShowTimeSeconds - day * 86400) / 3600);
? ?var minutes = parseInt((curShowTimeSeconds - (day * 86400) - (hours * 3600)) / 60);
? ?var seconds = curShowTimeSeconds % 60;
? ?renderDigit(DAY_MARGIN_LEFT, DAY_MARGIN_TOP, parseInt(day / 10), ctx);
? ?renderDigit(DAY_MARGIN_LEFT + 15 * (R + 1), DAY_MARGIN_TOP, parseInt(day % 10), ctx);
? ?renderDigit2(MARGIN_LEFT, MARGIN_TOP, parseInt(hours / 10), ctx);
? ?renderDigit2(MARGIN_LEFT + 16 * (r + 1), MARGIN_TOP, parseInt(hours % 10), ctx);
? ?renderDigit2(MARGIN_LEFT + 35 * (r + 1), MARGIN_TOP, 10, ctx);
? ?renderDigit2(MARGIN_LEFT + 46 * (r + 1), MARGIN_TOP, parseInt(minutes / 10), ctx);
? ?renderDigit2(MARGIN_LEFT + 62 * (r + 1), MARGIN_TOP, parseInt(minutes % 10), ctx);
? ?renderDigit2(MARGIN_LEFT + 81 * (r + 1), MARGIN_TOP, 10, ctx);
? ?renderDigit2(MARGIN_LEFT + 92 * (r + 1), MARGIN_TOP, parseInt(seconds / 10), ctx);
? ?renderDigit2(MARGIN_LEFT + 108 * (r + 1), MARGIN_TOP, parseInt(seconds % 10), ctx);

? ?for (var i = 0; i < littleLemon.length; i++) {
? ? ? ?ctx.fillStyle = littleLemon[i].color;
? ? ? ?ctx.beginPath();
? ? ? ?// lemon(ctx, 3, -3, littleLemon[i].x, littleLemon[i].y, r, -(Math.random() * 360));
? ? ? ?ctx.arc(littleLemon[i].x, littleLemon[i].y, r, 0, 2 * Math.PI);
? ? ? ?ctx.closePath();
? ? ? ?ctx.fill();
? ?}

}


//更新Lemon位置
function updateLemon() {
? ?for (var i = 0; i < littleLemon.length; i++) {
? ? ? ?littleLemon[i].x += littleLemon[i].vx;
? ? ? ?littleLemon[i].y += littleLemon[i].vy;
? ? ? ?littleLemon[i].vy += littleLemon[i].g;

? ? ? ?if (littleLemon[i].y >= height - r) {
? ? ? ? ? ?littleLemon[i].y = height - r;
? ? ? ? ? ?littleLemon[i].vy = -littleLemon[i].vy * 0.75;
? ? ? ?}
? ?}

? ?var cnt = 0;
? ?for (var i = 0; i < littleLemon.length; i++)
? ? ? ?if (littleLemon[i].x + r > 0 && littleLemon[i].x - r < width)
? ? ? ? ? ?littleLemon[cnt++] = littleLemon[i];

? ?while (littleLemon.length > Math.min(300, cnt)) {
? ? ? ?littleLemon.pop();
? ?}

}

//根據當前時間渲染數字(dd天數)
function renderDigit(x, y, num, ctx) {
? ?for (var i = 0; i < digit[num].length; i++) {
? ? ? ?for (var j = 0; j < digit[num][i].length; j++) {
? ? ? ? ? ?if (digit[num][i][j] === 1) {
? ? ? ? ? ? ? ?ctx.beginPath();
? ? ? ? ? ? ? ?// ctx.arc(x+j*2*(R+1)+(R+1),y+i*2*(R+1)+(R+1),R,0,Math.PI*2,true);
? ? ? ? ? ? ? ?// -(Math.random()*25);
? ? ? ? ? ? ? ?lemon(ctx, 3, -3, x + j * 2 * (R + 1) + R, y + i * 2 * (R + 1) + (R + 1), R, 0);
? ? ? ? ? ? ? ?// lemon(ctx,3,-3,400,400,100,-25);
? ? ? ? ? ? ? ?ctx.closePath();
? ? ? ? ? ?}
? ? ? ?}
? ?}
}

//根據當前時間渲染數字(hh,MM,ss)
function renderDigit2(x, y, num, ctx) {
? ?for (var i = 0; i < digit[num].length; i++) {
? ? ? ?for (var j = 0; j < digit[num][i].length; j++) {
? ? ? ? ? ?if (digit[num][i][j] == 1) {
? ? ? ? ? ? ? ?ctx.beginPath();
? ? ? ? ? ? ? ?// ctx.arc(x+j*2*(R+1)+(R+1),y+i*2*(R+1)+(R+1),R,0,Math.PI*2,true);
? ? ? ? ? ? ? ?// -(Math.random()*25);
? ? ? ? ? ? ? ?lemon(ctx, 3, -3, x + j * 2 * (r + 1) + r, y + i * 2 * (r + 1) + (r + 1), r, 0);
? ? ? ? ? ? ? ?// lemon(ctx,3,-3,400,400,100,-25);
? ? ? ? ? ? ? ?ctx.closePath();

? ? ? ? ? ?}
? ? ? ?}
? ?}
}






正在回答

舉報

0/150
提交
取消

curSeconds與nextSeconds相等,數組添加失敗

我要回答 關注問題
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號