IMPORTANCEThe bidirectional association between sleep duration and cognitive function has not been conclusively demonstrated.OBJECTIVETo investigate the longitudinal association between sleep duration and cognitive function among middle-aged and elderly Chinese participants.Design, SETTING, AND PARTICIPANTSA national representative and prospective longitudinal study in China. 7984 participants aged 45 years and above were assessed at baseline between June 2011 and March 2012 (wave 1) and 2013 (wave 2), 2015 (wave 3) and 2018 (wave4).MAIN OUCOMES AND MEASURESSelf-reported nighttime sleep duration was evaluated by interview. Cognitive function was evaluated via assessments of global cognition, which reflected the ability of episodic memory, visuospatial construction, calculation, orientation and attention.ResultsRegarding the 7984 participants in wave 4, the mean (SD) age was 64.7 (8.4), 3862 (48.4) were male, and 6453 (80.7) lived in rural area. There were 14981, 11768 (78.6%), 10192 (68.0%), 7984 (53.3%) participants in the four waves of the study, respectively. Latent growth models showed both sleep duration and global cognition worsen over time. Cross-lagged models indicated that long or short sleep duration in the previous wave was associated lower global cognition in the next wave (standardized β=-0.066; 95%CI: −0.073, −0.059; P<0.001; Wave 1 to 2), and lower global cognition in the previous wave was associated with long or short sleep duration in the next wave (standardized β=-0.106; 95%CI: −0.116, −0.096; P<0.001; Wave 1 to 2). Global cognition was probably the major driver in this reciprocal associations.CONCLUSIONS AND REVELANCEThere were bidirectional associations between long or short sleep duration and cognitive function. Lower cognitive function had a stronger association with worse cognitive function than the reverse. A moderate sleep duration is always recommended. Moreover, attention should be paid on the declined cognition and cognitive therapy among older adults with short or long sleep duration.