I have built a page that store a message and a user to send the message to in a db. The form has a restriction: you can't use the same user for the next 30 seconds from when you have posted the message to the db.
我已经构建了一个存储消息的页面和一个用户将消息发送到数据库中。表单有一个限制:从将消息发布到数据库后的30秒内,您不能使用相同的用户。
function undisable (id,time){
setTimeout(function() {
$('#'+id).prop('disabled', false);
}, (30000-time));
}
$('#destinatario option').each(function(){
var ds = $(this).attr('data-ts');
var dateArray = ds.split(" "); // split the date and time
var ds1 = dateArray[0].split("-"); // split each parts in date
var ds2 = dateArray[1].split(":"); // split each parts in time
var newDate = new Date(ds1[0], (+ds1[1] - 1), ds1[2], ds2[0], ds2[1], ds2[2]).getTime(); //parse it
var currentDate = new Date().getTime();
var diff = currentDate - newDate;
if(diff < 30000){
$(this).prop('disabled', true);
var id = $(this).attr('id');
undisable(id,diff);
}
});
Now I'd like to add a new condition: you cannot post to any user from the last minute of an hour to the first minute of the next one. So for example you cannot post from 10:59:00 to 11:01:00 (and so on for each hour of the day). What is the cleverest way to set the if clause to do this? What I am trying to build is something like:
现在我想添加一个新条件:你不能从一小时的最后一分钟到下一分钟的第一分钟发布给任何用户。因此,例如,您不能从10:59:00发布到11:01:00(以及当天的每个小时等等)。设置if子句的最聪明方法是什么?我想要构建的是:
if(now(minutes) is between 59 and 01){ //this is the point where I get stuck at
$('#sendbutton').prop('disabled', true); //I cannot post
}else{
$('#sendbutton').prop('disabled', false); //I can post
}
Thanks for any help!
谢谢你的帮助!
1 个解决方案
#1
You're right, that complicates things. Here's the code to solve your problem. Relies on setTimeouts to disable and enable the button, and you can edit the times you want to disable the button easily.
你是对的,这使事情变得复杂。这是解决问题的代码。依靠setTimeouts来禁用和启用按钮,您可以轻松编辑要禁用按钮的时间。
function time_disabler()
{
var time_disable = 59; // time in minutes to disable the button
var time_enable = 1; // time in minutes to enable to button
var current_mins = new Date().getMinutes(); // current time (minutes)
var current_secs = new Date().getSeconds(); // current time (seconds)
var total_seconds = current_mins * 60 + current_secs; // current time overall in seconds
var last_min_secs = time_disable*60; // time in seconds to disable the button
var first_min_secs = time_enable * 60; // time in seconds to enable the button
// if in between the two times, disable button
if((total_seconds >= last_min_secs && total_seconds < first_min_secs) || (total_seconds < first_min_secs && first_min_secs < last_min_secs))
{
$('#sendbutton').prop('disabled', true); //I cannot post
var time = (total_seconds >= first_min_secs)? first_min_secs + (60*60) - total_seconds : first_min_secs - total_seconds ;
// set time out to recall this function, which will enable it
var t = setTimeout(function (){
time_disabler();
}, time * 1000 );
} else { // if not between the times, disable it
$('#sendbutton').prop('disabled', false); //I can post
// set time out to recall this function, which will disable it
var t = setTimeout(function(){time_disabler();}, ((last_min_secs> total_seconds ? last_min_secs : last_min_secs + 60*60) - total_seconds) * 1000 );
}
}
time_disabler();
-- Old Answer --
- 旧答案 -
You would just use JavaScript's getMinutes() and getSeconds():
你只需使用JavaScript的getMinutes()和getSeconds():
var current_mins = new Date().getMinutes();
var current_secs = new Date().getSeconds();
current_mins += current_secs/60;
if(current_mins >= 59 || current_mins <= 1)
{
$('#sendbutton').prop('disabled', true); //I cannot post
} else {
$('#sendbutton').prop('disabled', false); //I can post
}
#1
You're right, that complicates things. Here's the code to solve your problem. Relies on setTimeouts to disable and enable the button, and you can edit the times you want to disable the button easily.
你是对的,这使事情变得复杂。这是解决问题的代码。依靠setTimeouts来禁用和启用按钮,您可以轻松编辑要禁用按钮的时间。
function time_disabler()
{
var time_disable = 59; // time in minutes to disable the button
var time_enable = 1; // time in minutes to enable to button
var current_mins = new Date().getMinutes(); // current time (minutes)
var current_secs = new Date().getSeconds(); // current time (seconds)
var total_seconds = current_mins * 60 + current_secs; // current time overall in seconds
var last_min_secs = time_disable*60; // time in seconds to disable the button
var first_min_secs = time_enable * 60; // time in seconds to enable the button
// if in between the two times, disable button
if((total_seconds >= last_min_secs && total_seconds < first_min_secs) || (total_seconds < first_min_secs && first_min_secs < last_min_secs))
{
$('#sendbutton').prop('disabled', true); //I cannot post
var time = (total_seconds >= first_min_secs)? first_min_secs + (60*60) - total_seconds : first_min_secs - total_seconds ;
// set time out to recall this function, which will enable it
var t = setTimeout(function (){
time_disabler();
}, time * 1000 );
} else { // if not between the times, disable it
$('#sendbutton').prop('disabled', false); //I can post
// set time out to recall this function, which will disable it
var t = setTimeout(function(){time_disabler();}, ((last_min_secs> total_seconds ? last_min_secs : last_min_secs + 60*60) - total_seconds) * 1000 );
}
}
time_disabler();
-- Old Answer --
- 旧答案 -
You would just use JavaScript's getMinutes() and getSeconds():
你只需使用JavaScript的getMinutes()和getSeconds():
var current_mins = new Date().getMinutes();
var current_secs = new Date().getSeconds();
current_mins += current_secs/60;
if(current_mins >= 59 || current_mins <= 1)
{
$('#sendbutton').prop('disabled', true); //I cannot post
} else {
$('#sendbutton').prop('disabled', false); //I can post
}