//このallowAjax = true;をコメントアウトするといいねできなくなる
とコメントアウトしておりますが、これについて質問があります。この理由としてはJSはこのページに来た段階で、JSの先頭にあるallowAjax = trueを読み込むがそれ以降は、ページ遷移がない限りはクリックイベントから読み込むので、コメントアウトすると永遠にallowAjaxがfalseであるためいいねができないということでしょうか。
JS
1<?php 2// カウント数取得関数 3function get_count($file) 4{ 5 $filename = 'data/'.$file.'.dat'; 6 $fp = @fopen($filename, 'r'); 7 if ($fp) { 8 $vote = fgets($fp, 9182); 9 } else { 10 $vote = 0; 11 } 12 return $vote; 13} 14?> 15<!DOCTYPE HTML> 16<html lang="ja"> 17 18<head> 19 <meta charset="utf-8"> 20 <meta name="viewport" content="width=device-width, initial-scale=1"> 21 <title>ajaxVote</title> 22 <link rel="stylesheet" href="style.css"> 23 <script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script> 24 <script> 25 $(function() { 26 allowAjax = true; 27 $('.btn_vote').click(function() { 28 if (allowAjax) { 29 allowAjax = false; 30 $(this).toggleClass('on'); 31 var id = $(this).attr('id'); 32 console.log(id); 33 //変数 = 条件式 ? trueの時の値 : falseの時の値 ; 34 $(this).hasClass('on') ? Vote(id, 'plus') : Vote(id, 'minus'); 35 } 36 }); 37 }); 38 39 function Vote(id, plus) { 40 cls = $('.' + id); 41 console.log(cls); 42 cls_num = Number(cls.html()); 43 console.log(cls_num); 44 count = plus == 'minus' ? cls_num - 1 : cls_num + 1; 45 $.post('vote.php', { 46 'file': id, 47 'count': count 48 }, function(data) { 49 if (data == 'success') cls.html(count); 50 setTimeout(function() { 51 //このallowAjax = true;をコメントアウトするといいねできなくなる 52 allowAjax = true; 53 }, 1000); 54 }); 55 } 56 </script> 57</head> 58 59<body> 60 61 <h1>ajaxVote</h1> 62 63 <article> 64 65 <section> 66 67 <p><img src="img/pic_01.jpg" width="100%" alt="綾北ダム"></p> 68 69 <div class="btn_area"> 70 71 <h2>綾北ダム</h2> 72 73 <p class="ico_heart vote_01"> 74 <?= get_count('vote_01') ?> 75 </p> 76 77 <p class="btn_vote" id="vote_01"></p> 78 79 </div><!-- /btn_area --> 80 81 </section> 82 83 <section> 84 85 <p><img src="img/pic_02.jpg" width="100%" alt="鶴田ダム"></p> 86 87 <div class="btn_area"> 88 89 <h2>鶴田ダム</h2> 90 <!-- 数字の横にある小さいハート --> 91 <p class="ico_heart vote_02"> 92 <?= get_count('vote_02') ?> 93 </p> 94 <!-- いいねに使うハート --> 95 <p class="btn_vote" id="vote_02"></p> 96 97 </div><!-- /btn_area --> 98 99 </section> 100 101 </article> 102 103 <footer> 104 105 <p>Copyright © 2016<br> 106 <a href="//klutche.org">klutche.org</a> Allrights Reserved.</p> 107 108 </footer> 109 110</body> 111 112</html> 113
回答1件
あなたの回答
tips
プレビュー