Ranged units are best used against light infantry or other ranged units. Archers and slingers will pin down skirmishers and prevent them and their javalins from coming in range of your main lines. In most armies they should still only be a small proportion, though.