LoginSignup
5
3

More than 5 years have passed since last update.

【Laravel】eventを2回以上呼ばないようにjQueryで制御する

Posted at

「送信ボタンを押してメールを送る」実装を以下のようにeventで実装している。
このままだと、ボタンを押した数だけeventが発動してしまうのでメールが何通も送られてしまう。

public function postMail()
{
    event(new MailEvent());
    return redirect()->route("index");
}

これを解消する1つの手段として、jQueryを使ってクリック時のeventを制御する方法がある。
ポイントとしては click dblclick(ダブルクリック)の2つの場合を考慮しておくこと。

$(()=>{
    var sent = false;
    $('.sendBtn').on("click", (e)=>{
        if(sent){
           e.preventDefault()
        }
        sent = true
    })
    $('.sendBtn').on("dblclick", (e)=>{
        e.preventDefault();
    })
})
5
3
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
3