We propose and study an active cooling mechanism for the nanomechanical resonator (NAMR) based on periodical coupling to a Cooper pair box (CPB), which is implemented by a designed series of magnetic flux pluses threading through the CPB. When the initial phonon number of the NAMR is not too large, this cooling protocol is efficient in decreasing the phonon number by 2 to 3 orders of magnitude. Our proposal is theoretically universal in cooling various boson systems of a single mode. It can be specifically generalized to prepare the nonclassical state of the NAMR.