I had some help today with a need and this is what I have come up with:
$(window).keypress(function (e)
{
if (e.keyCode == 13)
{
$('#doCheck')
.css('color', '#666')
.delay(100)
.queue(function (next)
{
$(this).css('color', '#00ff00');
next();
});
doCheck('mark1');
}
<input id='doCheck' type="button" onclick="doCheck('mark1'); return false;" value="Mark" />
The idea here is that when a user presses the ENTER key then the color of the button will change color for 100 milliseconds and at the same time I call a function called doCheck that will call AJAX.
My problem is that there seems to be some minimal delay of almost a second before the color of the button changes back to #666. I expect a much shorter time but even if I set the delay to 10 then it still seems to delay for 1/2 to one second. I should add that if I set a long delay like five seconds then it really does do the five seconds.
I can’t see what’s wrong here.
Does anyone have any ideas?
thanks,
Here is the doCheck function. The function works correctly. Just the button thing is giving me problems. Note that I just changed it to async: true
function doCheck(task)
{
var parms = {
task: task,
pk: $('#Q_PartitionKey').val(),
rk: $('#Q_RowKey').val(),
};
$.ajax({
type: "POST",
traditional: true,
url: '/power/check',
async: true,
data: parms,
dataType: "json",
success: function (data)
{
// $('#doCheckMark').attr("disabled", false);
// $('#doCheckMark').css("color", "#000");
},
error: function ()
{
var cdefg = data;
}
});
}
Is
doCheckdoing a synchronous call? If so, the queued function won’t get called until your ajax call returns… which might account for the unexpected delay.The fix would be to ensure that doCheck’s using a proper asynchronous call.