実現したいこと
1人につき、3回までしか表示されないおみくじをつくりたいと思っています。
前提
サイトに差し込みができるツールで、js-cookieを使って作りたいと思っています。
発生している問題・エラーメッセージ
表示回数を「count」に入れてその数で条件分岐をしていますが、
最初は「undefine」が入っているはずなのに、「undefine」ではない方の処理がされてしまいます。
該当のソースコード
html
1 2<style> 3 .popup-pr{ 4 position: relative; 5 } 6 .popup-ch{ 7 position: absolute; 8 left: 0; 9 top: 0; 10 background-color: #fff; 11 color: #333; 12 position: fixed; 13 right: 10px; 14 bottom: 100px; 15 z-index: 100000; 16 width: 60%; 17 height: 60%; 18 margin: auto; 19 } 20 .popup-ch p{ 21 color: #333; 22 } 23 </style> 24 <div class="gatya-popup-pr"> 25 <div class="gatya-popup-ch"> 26 <p>ガチャガチャを回す<br>ボタンをクリックしてください。</p> 27 <div class="wrapper-button"> 28 <button id="button" class="button">おみくじを引く</button> 29 </div> 30 <p id="result"></p> 31 </div> 32 </div> 33 <script> 34 window.addEventListener("load",function() { 35 var count = Cookies.get("count"); 36 if( count === "undefined"){ 37 var count = 1; 38 mattrz_jquery(".gatya-popup-ch").css("display","block"); 39 const list = { 40 "1等" : 0.02, 41 "2等" : 0.1, 42 "3等" : 99.88, 43 }; 44 document.getElementById("button").onclick = function () { 45 var random = Math.floor(Math.random() * 100); 46 var rate = 0; 47 for(var data in list){ 48 rate += list[data]; 49 if(random <= rate){ 50 document.getElementById("result").textContent = data; 51 document.cookie = 'r=data'; 52 return; 53 } 54 } 55 } 56 }else{ 57 count = count++; 58 if(count >= 4 ){ 59 mattrz_jquery(".gatya-popup-ch").css("display","none"); 60 } 61 } 62 }) 63 </script>
試したこと
if文の条件を(!count)にして、中身も反対にしてみましたが、反対の処理がされてしまいます。
すみませんが、どなたかご教示いただきたいです。よろしくお願いします。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2023/12/25 02:58