How do caterpillars "know" when it is time to turn into a chrysalis?

How do caterpillars "know" when it is time to turn into a chrysalis?

Caterpillars have a chemical called juvenile hormone in their bodies that is made by their brain. Whenever a caterpillar sheds its skin and the juvenile hormone level is high it goes to the next caterpillar stage. When the juvenile hormone level is low the caterpillar wanders to find a site to make a chrysalis (or a cocoon if its a moth), then it becomes a chrysalis and not another caterpillar stage.