Abstract Background Individual and environmental factors have been found to be related to cognitive function. However, few studies have examined the longitudinal effects of both individual and environmental factors over time. The purpose of this study was to examine the effects of individual and environmental factors over time on older people’s cognitive function. Methods Nationally representative panel data from the Taiwan Longitudinal Survey on Aging 1999–2015 (n = 6349 persons, observations = 12,042) were used. City-level indicator data were sourced from the government. A multilevel mixed linear model analysis was conducted. Results Better cognitive function was significantly related to individuals’ work, ethnicity, younger age, higher education level, better self-rated health, higher level of emotional support received, being more religious, higher economic satisfaction, and living in the cities with higher population densities. Education and social connectedness were protective factors over time. Conclusion Socioeconomics and social connectedness are related to cognitive function. A more social integrated lifestyle and financially secure living is suggested in the policy.