index.php
<!DOCTYPE html> <html lang="ja"> <head> <{$content_type|smarty:nodefaults}> <link href="<{$root_surl}>css/master.css" rel="stylesheet" type="text/css"> <script src="https://code.jquery.com/jquery-1.9.1.min.js"></script> <script src="../js/jquery.q.js" type="text/javascript"></script> <script> $(function(){ //---カウントダウン var timelimit = '<{$timelimit}>'; var m = getFinishTime(parseInt(timelimit, 10)); countDown(m); }); $(function(){ //---テスト $("ul>li").click(quesFunc); }); </script> </head> <body> <div id="Timer"></div> <ul> <li id="1">テスト1</li> <li id="2">テスト2</li> <li id="3">テスト3</li> </ul> <form id="nextquizBtn" action="<{$url}>" method="post" target="_self"> <input type="hidden" name="id" value="<{$id}>"> <input type="hidden" name="time" value=""> <input type="submit" value="次へ"> </form> </body> </html>
jquery.q.js
// --- カウントダウン function getFinishTime(e){ var datetime = new Date(); return datetime.setTime(datetime.getTime() + ((e) * 1000)); // 秒 } function countDown(m){ var startTime = new Date(); var endTime = new Date(m); var diff= endTime - startTime; var times= 24 * 60 * 60 * 1000; var sec= Math.floor(diff % times / 1000) % 60 % 60 if(diff > 0){ $("#Timer").text(sec); setTimeout(function(){ countDown(m); }, 10); }else{ // 終了した時のテキスト $("#Timer").text('Time Over !!'); } } // --- クリックアクション var selectID; function quesFunc(){ if (selectID == this.id) return; $(this).addClass("selected"); $("#" + selectID).removeClass("selected"); this.innerText = "xxxxx" }
▽1
●
a-kuma3 ●100ポイント ベストアンサー |
例えば、こんな感じ。
$(function(){ //---テスト $("ul>li").click(function() { quesFunc(); countDown(); }); });
jQuery 的には、こうした方が良いのかも。
$(function(){ //---テスト $("ul>li") .click(quesFunc) .click(countDown); });
目立つように改行してますが、一行に続けて書いても同じです。
$(function(){ //---テスト $("ul>li").click(quesFunc).click(countDown); });
var continue_count_down = true; // ★ // --- カウントダウン function getFinishTime(e){ var datetime = new Date(); return datetime.setTime(datetime.getTime() + ((e) * 1000)); // 秒 } function countDown(m){ var startTime = new Date(); var endTime = new Date(m); var diff = endTime - startTime; var times = 24 * 60 * 60 * 1000; var sec = Math.floor(diff % times / 1000) % 60 % 60 if(continue_count_down && diff > 0){ // ★ $("#Timer").text(sec); setTimeout(function(){ countDown(m); }, 10); }else if (diff > 0){ // ★ $("#nextquizBtn input[name='time']").val(Math.floor(diff / 1000)); // ★ }else{ // 終了した時のテキスト $("#Timer").text('Time Over !!'); } } // --- クリックアクション var selectID; function quesFunc(){ if (selectID == this.id) return; $(this).addClass("selected"); $("#" + selectID).removeClass("selected"); this.innerText = "xxxxx" continue_count_down = false; // ★ }
name="time" の type を "text" に変えて、実際に残り秒数が入るのを確認してみてください。