Page 1 of 7 123 ... LastLast
Results 1 to 10 of 62

Thread: Adding a time limit to a code

  1. #1
    Join Date
    Oct 2013
    Posts
    169
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Adding a time limit to a code

    I use this code in my headers that stops members from deleting their posts except for staff, is there a way to put a time delay in it? For instance if a member makes a post and within 5 minutes of making the post wants to delete it they can but after that they will not be able to delete it at all.

    The code:

    Code:
    <script type="text/javascript">
    $(document).ready(function(){
    if(!proboards.data('user').is_staff){
     $('.options_menu li').each(function(){
     if($(this).html().indexOf('Delete Post') != -1)
     $(this).remove();
     });
    }
    });
    </script>

  2. #2
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Generally that sort of thing is (can be) set in a forum's configuration. So, please double check that you cannot already take care of that in the config.

    Now, that said, we can easily delay execution of the code in your post. But that won't distinguish between a user returning to a page where they previously made a post perhaps hours or days ago versus a page where they have just now made a post. You might have a way to do that though (use the above code on a normal page, use the new timed code on the page a user lands on after making a new post). If you can control that, then just add a timeout for the code you wish to delay:

    Code:
    <script type="text/javascript">
    $(document).ready(function(){
    if(!proboards.data('user').is_staff){
     setTimeout(function(){
      $('.options_menu li').each(function(){
      if($(this).html().indexOf('Delete Post') != -1)
      $(this).remove();
      });
     }, 5 * 60 * 1000); // 5 minutes
    }
    });
    </script>
    But - what about a user who makes a new post, does something else on the forum for 3 minutes, and then comes back and wants to delete their post? With this code, there's no way we can tell they still have time left, or how much time they have left.

    If there's a place on the page that says when the post was made that can be read accurate to the minute (preferably second, but minute will roughly do) at least while the post is 6 minutes or less old, we could read that and remove the delete option if the post is older than five minutes, and set the timer for the rest of them relative to how much time is left until the age will be five minutes. But to do that, I would need to see the page.

    On one of your other pages I see things like:

    5 hours ago

    and:

    Jan 20, 2014 at 10:39pm

    showing the age of individual posts. How accurate is that and what does it show during the posts first 6 minutes of existence?
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  3. #3
    Join Date
    Oct 2013
    Posts
    169
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default

    Ok John with the other thread we've got going I haven't had time to come back to this one. First of all they don't have the configuration on a timed delete thing because they defend the post deleting by members vigorously and even though they don't try to stop forum owners from removing the delete post function they don't support it either.

    Now the question you had about 5 hours ago and the dates and time are their timestamps and on PB forums as soon as one posts the first mention of time after a post is "a few seconds ago" then it counts in one minute intervals as in "one minute ago" up to 60 minutes in which it changes to "one Hour ago" and then to the daily count and so on.

    But on my forums I have a footer code that puts the timestamp readings to exact time and date like listed here:

    http://scooterdoc.proboards.com/


    Here is the code it's a monster:
    Code:
    <script type="text/javascript">
    
    /* Replace elapsed time with specific time (Sept 15, 2013) */
    $(function(){
                    var a = 0, timeID = null, ac = arguments.callee, abbr = this.jquery? this : (this.ownerDocument ? $(this) : $('abbr.time'))
                    for(; a < $.livequery.queries.length; a++){
                                    if($.livequery.queries[a].selector == "abbr.time" && "function" == typeof $.livequery.queries[a].fn && $.livequery.queries[a].fn.toString().indexOf("non_mod_date") != -1)
                                    {              
                                                    timeID = $.livequery.queries[a]/*.id*/; $.livequery.stop(timeID.id);break; 
                                    }
                    }
                    //$('abbr.time')
                    abbr.each(function () {
                                    var time = $(this).addClass('modified_time').removeClass('time'), raw_date = new Date(parseInt(time.data("timestamp"),10));
                                    if($(this).hasClass('recent_time') && !/yesterday/i.test($(this).text())){                
                                                    time = time.removeClass('recent_time');
                                                    var  today = raw_date.getDate() == (new Date()).getDate()?"today":"yesterday",
                                                    user_date = ['M d, yy', 'd M, yy'][proboards.data("time_style") || 0];
                                                    time.replaceWith(
                                                                    $(this).clone(true).addClass('recent_time')
                                                                    .html(
                                                                                    '<span class="'+today+'">'+today+' </span>'
                                                                                    + '<span class="at">at</span> <span class="at-time">' +
                                                                                    (proboards.data('military_time') ? $.formatTime('HH:mm', raw_date) : $.formatTime('h:mmp', raw_date))
                                                                                    +'</span>'
                                                                    )
                                                    );
                                    }else if(/yesterday/i.test($(this).text())){
                                                    time.replaceWith(
                                                    $(this).clone(true)
                                                    .html(
                                                                    '<span class="yesterday">yesterday </span><span class="at">at </span><span class="at-time">' 
                                                                    + $(this).text().split("at ")[1] 
                                                                    +'</span>'
                                                    )
                                                    )
                                    }else{
                                                    if(time.text().indexOf(" at ")== -1)
                                                                    time.text($.datepicker.formatDate((proboards.data('time_style') ? 'd M yy' : 'M d, yy'), raw_date) + ' at ' + $.formatTime((proboards.data('military_time') ? 'HH:mm' : 'h:mmp'), raw_date))
                                                    time.replaceWith(function(){
                                                                    for(var dt = time.text().split(" at")[0], a = dt.split(/,?\s+/), b=0, c = $('<span></span>'); b<a.length; b++){
                                                                                    if((d = a[b].match(/^(\d+)$/))){
                                                                                                    if(d[1].length == 4)
                                                                                                                    c.append('<span class="year">'+a[b]+' </span>').addClass("year_"+a[b]);
                                                                                                    else
                                                                                                                    c.append('<span class="day">'+a[b]+' </span>').addClass("day_"+a[b])
                                                                                    }else{
                                                                                                    c.append('<span class="month">'+a[b]+' </span>').addClass("month_"+a[b])
                                                                                    }
                                                                    }
                                                                    c.append('<span class="at">at </span>').append('<span class="at-time">' + time.text().split(" at ")[1] + '</span>')
                                                                    return time.clone(true).html('').append(c)
                                                    })
                                    }
    
                    })
                    $('abbr.modified_time').addClass('time'); //re-enable livequery treatment
                    if(!ac.func){ 
                                    ac.func = function(){
                                                    $.livequery.run(timeID.id); 
                                                    ac();
                                    }
                    }
                    if(!ac.shoutbox){ ac.shoutbox = $('.shoutbox_messages')
                    .on('shoutbox_update.has-payload', function(event,data){
                                    if(data.shout.length){
                                                    data.shout.each(function(){
                                                                    var time = $('abbr.time',this)
                                                                    timeID.fn.call(time);
                                                                    ac.call(time.addClass('recent_time'));
                                                    })
                                    }
                    })}
                    if($.inArray(ac.func, proboards.events.afterSearch) == -1){ proboards.on("afterSearch", ac.func); }
    })
    
    </script>
    I'm confused about what you asked here?


    Now, that said, we can easily delay execution of the code in your post. But that won't distinguish between a user returning to a page where they previously made a post perhaps hours or days ago versus a page where they have just now made a post. You might have a way to do that though (use the above code on a normal page, use the new timed code on the page a user lands on after making a new post). If you can control that, then just add a timeout for the code you wish to delay:

  4. #4
    Join Date
    Oct 2013
    Posts
    169
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default

    Oh another thing John the code you put the timer in would need the staff portion removed wouldn't it? With the old code the delete button is still there to use for staff only.

  5. #5
    Join Date
    Oct 2013
    Posts
    169
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default

    Would still like to make this work if possible. Thanks

  6. #6
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Yeah, sorry. We were working together a little while back and I got busy with a lot on non-forum things. I'm back for now. Of the two things we were working on, this one seems easier, so let's see what we can do. As I remember, the page actually contains text that tells us how old each post is, right? If so, all we need to do is trap that text (set some variable equal to it or gain a reference to it in some way) compare it against all possible values/wordings it could have and determine how old the post is on that basis. Armed with that knowledge, the code could then either hide or allow to be seen the edit button. It will not work for non-javascript enabled users. I'm going to go back over the previous posts here and see if I still have enough to go on in making the general idea I just explained here work for this specific case.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  7. #7
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    OK, let's try this out. First get rid of:

    Code:
    <script type="text/javascript">
    $(document).ready(function(){
    if(!proboards.data('user').is_staff){
     $('.options_menu li').each(function(){
     if($(this).html().indexOf('Delete Post') != -1)
     $(this).remove();
     });
    }
    });
    </script>
    Then add the highlighted to the existing function for the time display:

    Code:
    <script type="text/javascript">
    
    /* Replace elapsed time with specific time (Sept 15, 2013) */
    $(function(){
                    var a = 0, timeID = null, ac = arguments.callee, abbr = this.jquery? this : (this.ownerDocument ? $(this) : $('abbr.time'))
                    for(; a < $.livequery.queries.length; a++){
                                    if($.livequery.queries[a].selector == "abbr.time" && "function" == typeof $.livequery.queries[a].fn && $.livequery.queries[a].fn.toString().indexOf("non_mod_date") != -1)
                                    {              
                                                    timeID = $.livequery.queries[a]/*.id*/; $.livequery.stop(timeID.id);break; 
                                    }
                    }
                    //$('abbr.time')
                    abbr.each(function () {
                                    var time = $(this).addClass('modified_time').removeClass('time'), raw_date = new Date(parseInt(time.data("timestamp"),10));
                                    if(!proboards.data('user').is_staff){
                                    	var elapsed = new Date().getTime() - raw_date.getTime();
                                    	if(elapsed > 5 * 60 * 1000){
                                    		$(this).parents('.info').eq(0).next('.controls').find('.options_menu li').each(function(){
    							if($(this).html().indexOf('Delete Post') != -1)
    							$(this).remove();
    						});
                                    	}
                                    }
                                    if($(this).hasClass('recent_time') && !/yesterday/i.test($(this).text())){                
                                                    time = time.removeClass('recent_time');
                                                    var  today = raw_date.getDate() == (new Date()).getDate()?"today":"yesterday",
                                                    user_date = ['M d, yy', 'd M, yy'][proboards.data("time_style") || 0];
                                                    time.replaceWith(
                                                                    $(this).clone(true).addClass('recent_time')
                                                                    .html(
                                                                                    '<span class="'+today+'">'+today+' </span>'
                                                                                    + '<span class="at">at</span> <span class="at-time">' +
                                                                                    (proboards.data('military_time') ? $.formatTime('HH:mm', raw_date) : $.formatTime('h:mmp', raw_date))
                                                                                    +'</span>'
                                                                    )
                                                    );
                                    }else if(/yesterday/i.test($(this).text())){
                                                    time.replaceWith(
                                                    $(this).clone(true)
                                                    .html(
                                                                    '<span class="yesterday">yesterday </span><span class="at">at </span><span class="at-time">' 
                                                                    + $(this).text().split("at ")[1] 
                                                                    +'</span>'
                                                    )
                                                    )
                                    }else{
                                                    if(time.text().indexOf(" at ")== -1)
                                                                    time.text($.datepicker.formatDate((proboards.data('time_style') ? 'd M yy' : 'M d, yy'), raw_date) + ' at ' + $.formatTime((proboards.data('military_time') ? 'HH:mm' : 'h:mmp'), raw_date))
                                                    time.replaceWith(function(){
                                                                    for(var dt = time.text().split(" at")[0], a = dt.split(/,?\s+/), b=0, c = $('<span></span>'); b<a.length; b++){
                                                                                    if((d = a[b].match(/^(\d+)$/))){
                                                                                                    if(d[1].length == 4)
                                                                                                                    c.append('<span class="year">'+a[b]+' </span>').addClass("year_"+a[b]);
                                                                                                    else
                                                                                                                    c.append('<span class="day">'+a[b]+' </span>').addClass("day_"+a[b])
                                                                                    }else{
                                                                                                    c.append('<span class="month">'+a[b]+' </span>').addClass("month_"+a[b])
                                                                                    }
                                                                    }
                                                                    c.append('<span class="at">at </span>').append('<span class="at-time">' + time.text().split(" at ")[1] + '</span>')
                                                                    return time.clone(true).html('').append(c)
                                                    })
                                    }
    
                    })
                    $('abbr.modified_time').addClass('time'); //re-enable livequery treatment
                    if(!ac.func){ 
                                    ac.func = function(){
                                                    $.livequery.run(timeID.id); 
                                                    ac();
                                    }
                    }
                    if(!ac.shoutbox){ ac.shoutbox = $('.shoutbox_messages')
                    .on('shoutbox_update.has-payload', function(event,data){
                                    if(data.shout.length){
                                                    data.shout.each(function(){
                                                                    var time = $('abbr.time',this)
                                                                    timeID.fn.call(time);
                                                                    ac.call(time.addClass('recent_time'));
                                                    })
                                    }
                    })}
                    if($.inArray(ac.func, proboards.events.afterSearch) == -1){ proboards.on("afterSearch", ac.func); }
    })
    
    </script>
    Browser and/or server caches may need to be cleared and/or the page refreshed to see changes. If there are problems, please leave the code in place so I can diagnose the problem. Also, if there's a problem, please describe it in as much detail as possible.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  8. #8
    Join Date
    Oct 2013
    Posts
    169
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default

    OK John I have this in place here:

    http://scootproftest.freeforums.net/...2&scrollTo=127

    I made a post as a regular member on that thread as 008 twice and in both instances the "delete post" button disappeared in 5 minutes after posting, brilliant! You have posted there too so would you post once to confirm this? BTW to see the "delete post" button click on the gear icon in the top right corner of the posting window, take note of the time you made the post and you will have the delete post button for 5 minutes, as soon as the 6 minute has passed it disappeared for me. But as staff the button remains as it should.

    Oh so as not to be guessing what part does one change to change the time limit settings? I tried this is IE11 if you have chrome could you check it in that browser?

  9. #9
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    OK, 'works' in Chrome. But I'm not satisfied because two situations are not yet covered. If you post and just wait with the page sitting there for 10 minutes or whatever, the delete button is still there. Or if you refresh and 5 minutes haven't passed yet and just let it sit there, the delete button remains until time is up AND you refresh. It's only if you refresh the page and 5 minutes have passed that the delete button goes away.

    Since the page initially reloads just after posting, these are really the same problem. So I've rewritten to take care of this. Changes/additions highlighted, also included the time allowed as a variable near the top in case you want to change it:

    Code:
    <script type="text/javascript">
    
    /* Replace elapsed time with specific time (Sept 15, 2013) */
    $(function(){
    	var howlongallowed = 5 * 60 * 1000; //5 minutes in milliseconds
    	function keepwatch($del, raw_time){
    		if(new Date().getTime() - raw_time > howlongallowed){
    			$del.remove();
    			return;
    		} else {
    			setTimeout(function(){keepwatch($del, raw_time);}, 300);
    		}
    	}
    
                    var a = 0, timeID = null, ac = arguments.callee, abbr = this.jquery? this : (this.ownerDocument ? $(this) : $('abbr.time'))
                    for(; a < $.livequery.queries.length; a++){
                                    if($.livequery.queries[a].selector == "abbr.time" && "function" == typeof $.livequery.queries[a].fn && $.livequery.queries[a].fn.toString().indexOf("non_mod_date") != -1)
                                    {              
                                                    timeID = $.livequery.queries[a]/*.id*/; $.livequery.stop(timeID.id);break; 
                                    }
                    }
                    //$('abbr.time')
                    abbr.each(function () {
                                    var time = $(this).addClass('modified_time').removeClass('time'), raw_date = new Date(parseInt(time.data("timestamp"),10));
                                    if(!proboards.data('user').is_staff){
                                    	var $del;
                            		$(this).parents('.info').eq(0).next('.controls').find('.options_menu li').each(function(i, del){
    						if($(del).html().indexOf('Delete Post') != -1){
    							$del = $(del);
    							return false;
    						}
    					});
                                    	if($del){keepwatch($del, raw_date.getTime());}
                                    }
                                    if($(this).hasClass('recent_time') && !/yesterday/i.test($(this).text())){                
                                                    time = time.removeClass('recent_time');
                                                    var  today = raw_date.getDate() == (new Date()).getDate()?"today":"yesterday",
                                                    user_date = ['M d, yy', 'd M, yy'][proboards.data("time_style") || 0];
                                                    time.replaceWith(
                                                                    $(this).clone(true).addClass('recent_time')
                                                                    .html(
                                                                                    '<span class="'+today+'">'+today+' </span>'
                                                                                    + '<span class="at">at</span> <span class="at-time">' +
                                                                                    (proboards.data('military_time') ? $.formatTime('HH:mm', raw_date) : $.formatTime('h:mmp', raw_date))
                                                                                    +'</span>'
                                                                    )
                                                    );
                                    }else if(/yesterday/i.test($(this).text())){
                                                    time.replaceWith(
                                                    $(this).clone(true)
                                                    .html(
                                                                    '<span class="yesterday">yesterday </span><span class="at">at </span><span class="at-time">' 
                                                                    + $(this).text().split("at ")[1] 
                                                                    +'</span>'
                                                    )
                                                    )
                                    }else{
                                                    if(time.text().indexOf(" at ")== -1)
                                                                    time.text($.datepicker.formatDate((proboards.data('time_style') ? 'd M yy' : 'M d, yy'), raw_date) + ' at ' + $.formatTime((proboards.data('military_time') ? 'HH:mm' : 'h:mmp'), raw_date))
                                                    time.replaceWith(function(){
                                                                    for(var dt = time.text().split(" at")[0], a = dt.split(/,?\s+/), b=0, c = $('<span></span>'); b<a.length; b++){
                                                                                    if((d = a[b].match(/^(\d+)$/))){
                                                                                                    if(d[1].length == 4)
                                                                                                                    c.append('<span class="year">'+a[b]+' </span>').addClass("year_"+a[b]);
                                                                                                    else
                                                                                                                    c.append('<span class="day">'+a[b]+' </span>').addClass("day_"+a[b])
                                                                                    }else{
                                                                                                    c.append('<span class="month">'+a[b]+' </span>').addClass("month_"+a[b])
                                                                                    }
                                                                    }
                                                                    c.append('<span class="at">at </span>').append('<span class="at-time">' + time.text().split(" at ")[1] + '</span>')
                                                                    return time.clone(true).html('').append(c)
                                                    })
                                    }
    
                    })
                    $('abbr.modified_time').addClass('time'); //re-enable livequery treatment
                    if(!ac.func){ 
                                    ac.func = function(){
                                                    $.livequery.run(timeID.id); 
                                                    ac();
                                    }
                    }
                    if(!ac.shoutbox){ ac.shoutbox = $('.shoutbox_messages')
                    .on('shoutbox_update.has-payload', function(event,data){
                                    if(data.shout.length){
                                                    data.shout.each(function(){
                                                                    var time = $('abbr.time',this)
                                                                    timeID.fn.call(time);
                                                                    ac.call(time.addClass('recent_time'));
                                                    })
                                    }
                    })}
                    if($.inArray(ac.func, proboards.events.afterSearch) == -1){ proboards.on("afterSearch", ac.func); }
    })
    
    </script>
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  10. #10
    Join Date
    Oct 2013
    Posts
    169
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default

    Ok John it's working nicely still in IE11 and FF, checked both of those. Also I'm a little confused?


    I see

    var howlongallowed = 5 * 60 * 1000; //5 minutes in milliseconds


    Is 5 here the place where the time limit can be changed?

Similar Threads

  1. exceed time limit to excecute function
    By newbieCoder in forum JavaScript
    Replies: 6
    Last Post: 02-06-2012, 06:02 PM
  2. Multiple Pinging With Time Limit
    By young_coder in forum PHP
    Replies: 2
    Last Post: 07-21-2010, 01:53 PM
  3. Adding Date/Time User Logs Into Site...
    By tomyknoker in forum PHP
    Replies: 1
    Last Post: 03-01-2007, 04:02 PM
  4. Replies: 0
    Last Post: 10-30-2005, 10:07 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •