2015-05-09 64 views
0

我已經構建了一個存儲消息和用戶的頁面,以便將消息發送到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); 
    } 
}); 

現在我想添加一個新的條件:你不能從一個小時的最後一分鐘到下一個的第一分鐘發佈到任何用戶。因此,例如,您不能從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 
} 

感謝您的幫助!

回答

1

你說得對,這使事情變得複雜。以下是解決問題的代碼。依靠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();

- 老回答 -

你只使用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 
 
}

+0

情況是一點點比較法。加載頁面後,我應該可以在兩個禁用的mnutes後發佈消息。就像我爲單件物品做了30秒一樣。 –

+0

編輯我的答案做你想做的! –

+0

謝謝! :)正在尋找一個提示,但你給了我一個完整的答案! –

相關問題