Schedule a task to run x seconds for y seconds?

I want to run a task for x seconds [duration] but i want it to be called every y seconds [interval]

For example, if i have duration 5 seconds and interval 1 second, the task will run every 1 second for an entire 5 seconds.

is it possible to do this? if so an example code would be appreciated.