Some people experience leg jerks when laying down which are known as hypnic jerks which are nothing serious.
Some things such as stress, anxiety, fatigue, caffeine and sleep deprivation, may increase the frequency or severity of hypnic jerks that you experience.
Not everyone experiences those hypnic jerks or leg jerks when lying down but if you do it's usually nothing serious but if you're worried you could see a doctor about it.
The doctor I went to told me that it was just a normal reaction that some people get at night and for me it was nothing serious.
I no longer experience those hypnic jerks but did so when I was experiencing more anxiety or drank to much caffeine.
So you might want to cut back on coffee or any other caffeine drinks before bed.