2012-03-13 9 views
0

私は次のタイプライタースクリプトをどのように停止するのかと思っています。私は何も気にせず、単にリンクがクリックされるとただちにアニメーションを止めることを望んでいません。jQueryのタイプライターアニメーションを停止するonclick

$(function() { 
var ch = 0; 
var item = 0; 
var items = $('.headline_origin li').length; 
var time = 1000; 
var delay = 40; 
var wait = 6000; 
var tagOpen = false; 

function tickInterval() { 
    if(item < items) { 
     var text = $('.headline_origin li:eq('+item+')').html(); 
     type(text); 
     text = null; 
     var tick = setTimeout(tickInterval, time); 
    } else { 
     clearTimeout(tick); 
    } 
} 

function type(text) { 
    time = delay; 
    ch++; 
    if(text.substr((ch - 1), 1) == '<') { 
     if(text.substr(ch, 1) == '/') { 
      tagOpen = false; 
     } 
     var tag = ''; 
     while(text.substr((ch - 1), 1) != '>') { 
      tag += text.substr((ch - 1), 1); 
      ch++; 
     } 
     ch++; 
     tag += '>'; 
     var html = /\<[a-z]+/i.exec(tag); 
     if(html !== null) { 
      html = html[0].replace('<', '</') + '>'; 
      tagOpen = html; 
     } 
    } 
    if(tagOpen !== false) { 
     var t = text.substr(0, ch); 
    } else { 
     var t = text.substr(0, ch); 
    } 

    $('h1 span.origin').html(t); 
    if(ch > text.length) { 
     item++; 
     ch = 0; 
     time = wait; 
    } 
} 

var tick = setTimeout(tickInterval, time); 
}); 

ありがとうございます!あなたのtickInterval関数の内部

@rrfive

答えて

1

、setTimeoutメソッドからvar宣言を削除する - 私たちは、グローバルダニの変数を再利用することができます。

あなたが好きなボタンのクリックハンドラーにはclearInterval(tick);が必要です。

関連する問題