tags:

views:

81

answers:

2

hi, i am using php,mysql,jquery.

When a user clicks on save i go to back end and save data and show the response. once show the success i need to disable the save button for say 1 min. how do i do it.?

here is ma code

$(document).ready(function() {

 if ($.cookie('<?php echo $userId ?><?php echo $product ?><?php echo $alias ?>agreedisabledTime') > 0) {


    var remainingTime = (new Date()).getTime() - $.cookie('<?php echo $userId ?><?php echo $product ?><?php echo $alias ?>agreedisabledTime');


 if (remainingTime < <?php echo $timer ?>) {
  //if (remainingTime < 7200) {
       $('#agree').attr('disabled', 'disabled');
       $('#disagree').attr('disabled', 'disabled');
       setTimeout(function() { $('#agree').attr('disabled', ''); }, remainingTime);
       setTimeout(function() { $('#disagree').attr('disabled', ''); }, remainingTime);

    }
  }

$('#agree').click(function()
{
$('#response').show();
$.post('/all_include_files/increment.php',{update:'agree',product:"<?php echo $product?>",alias:"<?php echo $alias ?>"},function(data)
{
if(data.indexOf('ERROR') < 0)
{
$('#response').hide();
$('.agree_number').html(data);
alert('Thanks for your esteemed opinion');
 $('#agree').attr('disabled', 'disabled');
 $('#disagree').attr('disabled', 'disabled');

setTimeout(function() { $('#agree').attr('disabled', ''); }, "<?php echo $timer ?>");
setTimeout(function() { $('#disagree').attr('disabled', ''); }, "<?php echo $timer ?>");

$.cookie('<?php echo $userId ?><?php echo $product ?><?php echo $alias ?>agreedisabledTime', (new Date()).getTime());
}
else
{ alert(data); }
});

});

});

where $timer is set to 120000

+2  A: 

Disable button for 1 second:

if (success) {
 $('#buttonid').attr('disabled', 'disabled');
 setTimeout(function() { $('#buttonid').attr('disabled', ''); }, 1000);
}

EDIT about keeping track of browser reloads
You can easily access cookies with the jQuery plugin: http://plugins.jquery.com/project/cookie

if (success) {
 $('#buttonid').attr('disabled', 'disabled');
 setTimeout(function() { $('#buttonid').attr('disabled', ''); }, 1000);
 $.cookie('disabledTime', (new Date()).getTime());
}

Then read it upon loading the DOM:

$(document).ready(function() {
  if ($.cookie('disabledTime') > 0) {
    var remainingTime = (new Date()).getTime() - $.cookie('disabledTime');
    if (remainingTime > 0) {
       $('#buttonid').attr('disabled', 'disabled');
       setTimeout(function() { $('#buttonid').attr('disabled', ''); }, remainingTime);
    }
  }
});

Note that I didn't test this, but you get the idea

baloo
when he refreshes/(closes->opens) his browser , will the timer be still active.
pradeep
@pradeep: Nope, the execution environment will be reset when the page is refreshed.
Matti Virkkunen
ok forget after close-open of browser. can i set a session/ cookie using jquery
pradeep
@pradeep ~ You could... sort of. A session only exists on the server (that's the intent of session, something that happens "over there" while I'm working over here, it's a way of providing state to a stateless environment). The cookie is a function of the browser, although the server can send requests to set and read cookies. So, since jQuery runs in the browser, it most certainly can set a cookie. Also, a piece of advice on SO, if you goto reply to someone, using @baloo would get his attention much faster than just a reply without. It triggers the new message mechanism. Hello javascript ;)
drachenstern
@drachenstem - okie so u advice to set a cookie?can u give me a piece of code for it.. and how do i check cookie variable using jquery
pradeep
@drachenstern Well hello! @pradeep I've edited my post to reflect the reloading problem.
baloo
@pradeep and feel free to improve your accept rate :)
baloo
@baloo ~ You too? :-) ;-)
drachenstern
thanks for the help .that worked like a charm....
pradeep
is the value for 1 minute = 60000 ???
pradeep
@pradeep that's correct. @drachenstern what do you mean?
baloo
@drachenstern its working well..but when i refresh the page . the buttons are getting enabled . any problem?
pradeep
@baloo ~ the "improve your accept rate" ... seems a lot of site "visitors" truly only visit... that's all.
drachenstern
@pradeep ~ I sense a general disconnect here. You do realize that web apps are stateless right? They only know what you tell them. So if you want the button to be disabled, you have to tell it to be disabled. I think you're missing something here. When I reload the page, I am truly reloading the page. I'm starting over from scratch. I'm erasing whatever existed before, and getting something fresh. So if you want to stop them from pressing on reload, you have to make that part of the page load logic. There's a reason it's called a page, think of it like a sheet of paper. A new page is blank...
drachenstern
i have written code like this if ($.cookie('<?php echo $userId ?><?php echo $product ?><?php echo $alias ?>agreedisabledTime') > 0) { var remainingTime = (new Date()).getTime() - $.cookie('<?php echo $userId ?><?php echo $product ?><?php echo $alias ?>agreedisabledTime');alert(remainingTime); if (remainingTime < "<?php echo $timer ?>") {alert(remainingTime);
pradeep
$('#agree').attr('disabled', 'disabled'); $('#disagree').attr('disabled', 'disabled'); setTimeout(function() { $('#agree').attr('disabled', ''); }, remainingTime); setTimeout(function() { $('#disagree').attr('disabled', ''); }, remainingTime); } }
pradeep
after some 120000 seconds it gets enabled properly. but when i refresh the page it gets disabled again for some time .
pradeep
@drachenstern - please see my code
pradeep
$('#agree').click(function(){$('#response').show();$.post('/all_include_files/increment.php',{update:'agree',product:"<?php echo $product?>",alias:"<?php echo $alias ?>"},function(data){if(data.indexOf('ERROR') < 0){$('#response').hide();$('.agree_number').html(data);alert('Thanks for your esteemed opinion'); $('#agree').attr('disabled', 'disabled'); $('#disagree').attr('disabled', 'disabled');
pradeep
setTimeout(function() { $('#agree').attr('disabled', ''); }, "<?php echo $timer ?>");setTimeout(function() { $('#disagree').attr('disabled', ''); }, "<?php echo $timer ?>");$.cookie('<?php echo $userId ?><?php echo $product ?><?php echo $alias ?>agreedisabledTime', (new Date()).getTime());}else{ alert(data); }});}); is ma code where i set the cookie
pradeep
where $timer is set to 120000
pradeep
@pradeep - um, you really need to edit the original question and use the code structure element of the page.
drachenstern
@pradeep - in future, using comments to post code *isn't* the best way to explain - at best they are very hard to read. In some languanges: completely impossible. If it is key to the question, it would be better to edit the original question.
Marc Gravell
@drachenstern - have edited the original post . please see the code if some problem exists.
pradeep
+1  A: 

Instead of disabling it for a period of time, I would use the callback on completion of the command to enable the button, and disable it once you start the command. I would also remember to enable it if an error occurred, with an error popup to the user that it failed and that repeated failures are likely to imply the system is offline or has an error.

drachenstern
+1 For the better approach if he want to be able to save again as soon as the first request is done. But it might be another usecase
baloo
@baloo ~ Yeah, I know, but I think it's fairly obvious that a save button would want to be called repeatedly, and some mechanism that says "I'm still saving!" is always helpful. Microsoft usually puts an icon or changes the cursor, as do others, and while saving to a HDD is usually "fast enough" that the user doesn't notice, we should always provide a visual indicator for their notification on state changing instructions, yeah? So I agree, may not be his usecase, but I thought I'ld add a little educational material. Reviewing the question leads me to believe it's helpful at this point ;)
drachenstern