This paper mainly discusses distributed constrained optimization problem for second‐order multi‐agent system under undirected communication network. The task of all agents is to minimize the sum of the local convex functions, where each agent is individual and only accesses to one objective function. Different from the most existing results, where the objective functions are assumed to be time‐invariable, this paper considers the situation of time‐varying objective function. Besides, we don't require that the Hessian matrices are identical and the gradients are bounded. First, a novel time‐varying optimization algorithm is proposed based on the projection algorithm. Second, by using convex analysis and Lyapunov theory, it is shown that the states of all agents can reach consensus and asymptotically converge to the neighborhood of the optimal solution. Finally, some numerical examples are given to verify the effectiveness of our algorithms.