3
You’d really want to check this on the server, so it doesn’t matter if someone disables javascript.
If you’re using PHP one option is to set a time limit between votes.. eg only allow one vote per minute.
So on every vote, store the time of the vote in a session variable, then ignore subsequent votes if it is within the time limit:
//on vote
$now=date('U');
if(!isset($_SESSION['lastvote']) || $now - $_SESSION['lastvote']<LIMIT){
$_SESSION['lastvote']=$now;
// do database call
}else{
//throw error
}
1
Probably the easiest approach is to disable the buttons while a single operation is running.
To obtain this, assuming you’re using a JQuery ajax request to call the “upvote” / “downvote” method you simply have to:
$("#upvoteButton").click(function)
{
$("#upvoteButton").attr("disabled");
$.ajax({
url: 'url/upvote.extension',
success: function(data) {
$("#upvoteButton").removeAttr("disabled");
}
});
}
This way a single request is send & resolved per single tab / window.
0
If you trust that your users accept cookies, you could store the click together with the object id and a timestamp in the session, and test whether the user clicked the vote button during the last n seconds.
You also could disable the vote button on the web page with javascript, and enable it again after n seconds to revoke the vote.
Both suggestions are dependent on users who use a javascript enabled or cookie enabled browser.
If you have to take bots into consideration who just send the url repeatetly to the server, you need to take a different approach – e.g. check whether the current ip address has called the url in the last n seconds and raise a 403 Forbidden http error or something.
- [Django]-Transform JavaScript 2D Array
- [Django]-Djangos TEMPLATE_DIRS not found
- [Django]-Django – get max values over several periods
0
If you can use setTimeout
you can try something like this:
var toggleVote = (function () {
var voted = false,timeout;
return function () {
if( timeout )
clearTimeout(timeout)
voted = !voted
timeout = setTimeout(function() {
console.log("Ajax call - vote : " + voted)
},400)
}
})()
document.getElementById("vote").addEventListener("click",toggleVote)
Heres an example on JSBin
Try mashing the “Click me” div
- [Django]-Django: collecting users, objects activity stream
- [Django]-What is the equivalent for, SELECT * FROM table_name WHERE id IN (1,2,5,9) in Django
- [Django]-Copy a abstract model to another instance?
- [Django]-Django – implementation of select2 with url
0
Easy way:
<button id="upvote" onclick="upvote(this)">Up</button>
<script>
var upvote = function(x){
$(x).removeAttr('onclick'); //if you click this button again, it will do nothing
//call ajax to do something here...
//...............
//after finishing, add attribute onclick to the button
$(x).attr('onclick', 'upvote(this)')
};
</script>
- [Django]-Get models ordered by an attribute that belongs to its OneToOne model
- [Django]-Using Django-Q in production
- [Django]-How to improve 2 million data query speed in Django RESTful APIs
- [Django]-How to add django template variable in <img src>?
- [Django]-AttributeError: 'module' object has no attribute 'Datefield'