Không thể gọi đến hàm trong jquery

e viết hàm này để setTimeout đếm ngược thời gian được trả về từ server

function countDownTime(time){
	var timeFull=time*60*1000; 
	var minutes = Math.floor((timeFull%(1000 * 60 * 60)) / (1000 * 60));
    var seconds = Math.floor((timeFull%(1000*60)) / 1000);
    document.getElementById('m').innerText = minutes.toString();
    document.getElementById('s').innerText = seconds.toString();
    if (seconds === -1){
    	minutes -= 1;
        seconds ==59;
    }
    setTimeout(function(){
	    	seconds--;
	    	countDownTime(time);
	    }, 1000);
}

xong em gọi nó vào hàm này nhưng nó báo lỗi.

    if(!(surveyObj && surveyObj.listQuestion.length > 0)){
		return;
	}
	countDownTime(surveyObj.surveyTime);
	console.log(surveyObj);
	if(surveyObj.random==true){
		shuffle(surveyObj.listQuestion);
	}
	console.log(surveyObj);
	
	
	var htmlArr = [];
	htmlArr.push('<div id="survey-id" style="display:none">'+surveyObj.testId+'</div>');
	htmlArr.push('<div id="survey-name"><h2>'+surveyObj.name+'</h2></div>');
	htmlArr.push('<div class="timeout" float:right><span>Thời gian còn lại: </span><span id="m">Phút</span> : <span id="s">Giây</span></div>');
	
	console.log(surveyObj.surveyTime);
	htmlArr.push('<div class="question-list">');
	surveyObj.listQuestion.forEach(question=>{
		htmlArr.push('<div id="'+question.questionId+'" class="question">'); 
		htmlArr.push('<h3>'+question.content+'</h3>');
		htmlArr.push('<div class="answer-list">');
		question.listAnswer.forEach(answer=>{
			htmlArr.push('<div class="answer">');
			htmlArr.push('<input type="checkbox" class="answer-input" id="answer-'+answer.answerId+'" value="'+answer.answerId+'" onchange="answerAs('+question.questionId+','+ answer.answerId+')"> ');
			
			htmlArr.push('<label for="answer-'+answer.answerId+'">'+answer.content+'</label>');
			htmlArr.push('</div>');
		});
		htmlArr.push('</div>');
		htmlArr.push('</div>');
	})
	htmlArr.push('</div>');
	
	console.log(resultObject);
	htmlArr.push('<button class="btn btn-success btn-next" type="button" data-index="0">Tiếp theo</button>');
	htmlArr.push('<button class="btn btn-success btn-finish" type="button" style="display:none;">Hoàn thành</button>');
	
	jQuery('#survey').append(htmlArr.join('\n'));

Báo lỗi gì vậy bạn?

Mình thấy hàm đếm ngược có gì đó không ổn:

  • Tại sao là seconds-- mà không phải là time--?
  • Giá trị time là đơn vị tính bằng phút phải không? Rồi bạn đổi sang mili-giây. Sao không gửi giá trị bằng mili-giây hoặc giây từ máy chủ luôn cho xong.
  • Dùng setInterval() thấy ổn hơn.
  • Khi đếm về 0 thì làm gì?
2 Likes

dạ… nhưng khi em document.getElementById('m').innerText = minutes.toString(); để hiện nó lên nhưng nó ko lên… em đã thay đổi theo a góp ý

Nó chưa có thì sao mà chạy được chứ?
Đoạn chạy đếm ngược trước đoạn thêm html. Chạy đếm ngược gặp lỗi và dừng hàm luôn.
Để đoạn đếm ngược sau đoạn thêm html nhé.

jQuery('#survey').append(htmlArr.join('\n'));
countDownTime(surveyObj.surveyTime);
3 Likes
83% thành viên diễn đàn không hỏi bài tập, còn bạn thì sao?