ben_aaron
ben_aaron

Reputation: 1532

Use getTime in fadeIn function jquery

Ok, now I'm stuck for some time with this: I'm running a js script which displays words.

With some help from SO this works exactly as I want and seems much simpler than my previous attempts.

$("#stimuli").text("A WORD").fadeIn().delay(displaytime).fadeOut();}

Yet there is one issue, I want to record the time of the 'stimuli' appearance as t1, like

t1 = (new Date()).getTime();

and the time of a keypress as time t2. Is there a way to incorporate the t1 into the one-line above?

Upvotes: 0

Views: 49

Answers (2)

Todd Yandell
Todd Yandell

Reputation: 14696

Do you want the time to be recorded as soon as the element starts fading in or when the fade is done?

In the first case, just run those two lines together:

$('#stimuli').text('A WORD').fadeIn().delay(displayTime).fadeOut();
t1 = (new Date).getTime();

fadeIn, delay, and fadeOut run asynchronously, so the second line actually happens before the other methods.

To record the time after the fade is done, you need a callback:

$('#stimuli')
  .text('A WORD')
  .fadeIn(function () {
    t1 = (new Date).getTime();
  })
  .delay(displayTime)
  .fadeOut();

Upvotes: 1

Jason
Jason

Reputation: 52527

You could try something like this:

var elem = $('#stimuli'), t1;
elem.text('A WORD').fadeIn(function() {
    t1 = (new Date()).getTime();
    //do whatever recording of t1 here
}).delay(displaytime).fadeOut();

Upvotes: 1

Related Questions