Rodney Dangerfield spent two terrifying weeks awaiting brain surgery — and the courageous comic passed the time cracking jokes and putting the finishing touches on his autobiography.

Dangerfield, 81, underwent a delicate 12-hour operation to improve blood flow to his brain April 8 and was in stable condition at UCLA Medical Center a week later.

“Rodney wanted to get the surgery over and done with as soon as possible, but he was forced to wait two weeks for his doctor to return from vacation,” a close friend said.

“The wait was agonizing, but he spent it in true Rodney fashion, telling jokes and trying to lift the spirits of those around him. He also spent part of the time going over the galley proofs of his autobiography, ‘It’s Not Easy Being Me,’ that may be published under the title, ‘Rodney Exposed.'”

The bug-eyed actor-comedian has suffered a string of health problems in recent years.

He had an aortic aneurysm repaired in 1992 and a double bypass surgery in 2000. And, he had a mild heart attack on his 80th birthday — Nov. 22, 2001.

Dangerfield underwent the brain bypass surgery to prepare him for a heart valve replacement operation doctors say he desperately needs within a few weeks, his friend said.

“Tests showed he wouldn’t survive the valve surgery unless he first had an operation to increase the flow of blood to his brain,” the friend revealed.

Lying in a hospital bed with the first of two tricky operations behind him, the “Back to School” star continues to keep others in stitches with his famous one-liners, sources say.

“I have a thing for my nurse,” he told a visitor. “It’s a little thing, but . . .”

Though Dangerfield can’t receive cards or flowers in intensive care, a pile of get-well messages from friends and fellow performers have arrived at his home and at the hospital.

“Everyone from his house cleaner to Jay Leno and Jim Carrey have sent their best wishes,” his friend said. “Rodney is so elated that people haven’t forgotten him in his time of need.

“He said, ‘When you have people rooting for you, it makes you twice as determined to get through it.’ The way he looks at it, he’s 50 percent on his way to a full recovery — one surgery down and one to go.”