Home > other >  How to set limit amount of clicks button can be pressed using cookies
How to set limit amount of clicks button can be pressed using cookies

Time:10-14

I want to know how to set limit of clicks in button using cookies

CodePudding user response:

I think you have to use local storage

Here`s a simple example

const oldCount = localStorage.getItem('buttonCount');
localStorage.setItem('buttonCount', oldCount   1);
const newCount = localStorage.getItem('buttonCount');

if(newCount < 3){
//your func
}

CodePudding user response:

<!DOCTYPE html>
<html>
<head>
    <title>Limit Clicks Button</title>
    <link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous">
    <script
  src="https://code.jquery.com/jquery-3.6.0.min.js"
  integrity="sha256-/xUj 3OJU5yExlq6GSYGSHk7tPXikynS7ogEvDej/m4="
  crossorigin="anonymous"></script>
</head>
<body>

<button id="button" class="btn btn-success"> Enter </button>
</body>
</html>
<script type="text/javascript">
    
    localStorage.setItem("limit_click",10)
    localStorage.setItem("count",1)
$("#button").on("click",function(){
    var limit = localStorage.getItem("limit_click");
    let count = parseInt(localStorage.getItem("count"));    
    (count>=limit)?alert('opps'):localStorage.setItem("count",parseInt(count) 1);
    

}); 

</script>
  • Related