开发者

PHP/JS : Preventing doubleinserting with function

Yes im having a issue i thought i didn't have.

When you submit it runs a javascript function, that runs an ajax call. This function have this right under it´s name(first line in the function):

 $('#submitWall').attr('disabled', true);

This works very well, but not so well for the faster humans...I tried to click/pressing enter really fast, and it inserted to the database 2-3 times.

And I want to prevent this. As i said i have the above, which didn't solve it 100%.

Is there a solution for this in JS/jquery or maybe in PHP, so there's like a 1 second timelimit somehow..?

Here's my function:

    function DoWallInsert(BuID, uID){
     $('#submitWall').attr('disabled', true);
  var wrapperId = '#wall_insert';
    $.ajax({ 
       type: "POST",
       url: "misc/insertWall.php"开发者_JAVA技巧,
    data: {
    value: 'y',
    BuID : BuID,
    uID : uID,
    message : $('#message').val() 
    },
       success: function(msg){
$('#submitWall').attr('disabled', false);
     $(wrapperId).prepend(msg);
     $(wrapperId).children().first().slideDown('slow');
     $('#message').val("");
        }
     });
}


I think the problem here is that the attribute change does not come into effect until the function ends due to the fact that javascript runs single thread.

If you setTimeout to another function with minimum delay and have that function do all the rest of the processing, and maybe also have the first function test the attribute first and bailout if it is already disabled it might prevent this.

Since Javascript is single thread, even if the first function is run twice in a row before gui is updated the second call should still see it as disabled and ignore your click.

It might even be enough to check the attribute first, and if it is disabled, exit function.

 if($('#submitWall').attr('disabled'))
      return ;


Don't just set the attribute to disabled, also remove the onclick value. Then, use setTimeout() to replace it after X time.

document.getElementById('submitWall').onclick='';
setTimeout("document.getElementById('submitWall').onclick='DoWallInsert';", 1000);

That code may need tweaking, I'm not on my web development machine and can't test it.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜