How to figure out how to use setInterval correct with this jQuery script

This is a continuation of a previous question I asked.

I’m trying to display a clock based on a pre-determined time value .. not the current clients time.

Here’s my jQuery:

$(document).ready(function () {
    var currentTime = new Date('3/09/2010 9:27:29 PM');
    setInterval("DisplayTime(currentTime, $('.answer-body'))", 1000);
})

function DisplayTime(currentTime, destination) { ... }

Now inside the DisplayTime function, i was showing some custom text, calling the destintion.html(..) to display that custom text. And finally, after I display the text, I was thinking of adding 1 second to currentTime so when the next iteration of the interval, it’s not using the original time value, but 1 second later.

Problem: I cannot pass in the currentTime variable to the setInterval function. I don’t particularly want to have an anonymous function here, unless I have no choice.

How can I refactor my bad code?

So every second, the time is re-displayed with the new second being added.

10 thoughts on “How to figure out how to use setInterval correct with this jQuery script”

Leave a Comment