Maybe it's a plot device to create tension or add some kind of background noise.
It could be that the dog barking forever is part of a magical or fantasy element in the cartoon. Or perhaps it's to represent a never-ending problem or annoyance.
Maybe it saw something that made it feel threatened or excited.
Maybe it saw something that made it excited or scared.
It could be barking to protect its territory or to get attention. Sometimes dogs bark just because they're excited or trying to communicate something.
Maybe the dog is excited about going on a trip or is trying to get the attention of its owner.
Maybe the dog is excited or scared by something in the car.
Maybe the dog is excited or sees something interesting and is trying to communicate that through barking while being on the leash.
The dog might bark at the mailman in the cartoon because it's instinctively wary of strangers. Or maybe the dog is excited and just expressing itself in a way that seems like barking at the mailman.
It could be that the dog sees the elephant as a threat or is simply being playful and barking for fun. Sometimes in cartoons, animals act in unexpected ways for entertainment.
Maybe it saw something that scared or threatened it. That could make a dog bark angrily in a cartoon.
Maybe the dog sees the mailman as an intruder and is trying to protect its territory.