Nighttime leg cramps happen when a person isn't very active or asleep. Tired or dehydrated muscles become irritated and are more likely to cramp up, a Harvard article stated, adding that deficiency of electrolytes like magnesium or potassium in diet can cause frequent cramping. Sitting for long periods of time, poor postures and overusing muscles might also cause cramps.