简体   繁体   中英

Using setTimeout with a function that is using .animate

My function that uses .animate is working nicely but I'm trying to add in a setTimeout (probably 2-3 seconds) and not making much progress.

Below is the code for the animation.

tt = $('#tickertext');
tt.append('<pre>' + 'Paolo Nutini: New Shoes');
console.log('width ' + tt.width());
w = parseInt(tt.width());
tt.animate({
  left: w * -1
}, w * 10, 'linear', backToStart);
$('#ticker').css('width', '155px').css('visibility', 'visible');
$('#tickerholder').css('width', '155px');

function backToStart() {
  tt.animate({
      left: 155 
  }, 1)
  .animate({
      left: w * -1
  }, (w + 155) * 10, 'linear', backToStart);
}

Now below shouldn't I be able to do something like:

setTimeout("backToStart", 3000);

What exactly am I missing here? The above is having no effect.

looks like the method is in a closure scope, so try

setTimeout(backToStart, 3000);

even the string parameter is wrong because in that case you need to invoke the function like setTimeout("backToStart()", 3000); , but then the method backToStart must be in the global scope

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM