5

我试图让这个功能在它的循环结束时重复。我尝试将函数分配给一个变量并在回调中调用该变量,但失败了。我尝试将此函数包装在一个setInterval函数中,但仍然无法使其工作。

我如何让这个函数运行一个无限循环并重复自己?

$("span.text-change").typed({
     strings: ["First sentence.", "Second sentence."],
     typeSpeed: 30, // typing speed
     backDelay: 500, // pause before backspacing
     callback: function () {
         // do stuff
     }
 });

这是插件: Typed JS

jsFiddle在这里

4

6 回答 6

5

Typed.js 作者在这里。我认为像我的代码这样的人非常棒,足以挖掘它并使更多功能发挥作用。我绝对打算让循环成为您可以设置的默认功能,但现在这里有一个修复。

http://jsfiddle.net/LSsZr/8/

主要变化

self.stopArray = self.strings.length;
// instead of self.stopArray = self.strings.length-1;

这使得 stopArray 变量允许它在键入最后一个字符串后继续退格。否则,一旦打印最后一个,它将停止。


// if (self.arrayPos == self.stopArray && curStrPos == curString.length){
// animation that occurs on the last typed string
// place any finishing code here
//  self.options.callback();
//    clearTimeout();
// }

以上已删除。它检查输入的最后一个字符串,并在满足条件时触发一个函数。对于一个循环,我们不希望这样。


else{
   self.arrayPos = 0;
   self.typewrite(self.string, self.strPos);
}

上述内容已作为 else 添加到以下 if 语句中...

if (self.arrayPos < self.strings.length){

因此,如果数组位置小于数组中字符串的数量,请键入它们。否则,将位置重置为 0,然后再次运行原始函数。

继续使用 jsfiddle 中的代码作为你的 typed.js 文件,然后在你想要的页面上正常运行。虽然不再有 callback() !

让我知道你的想法/问题!

编辑 看起来有人在这里向插件添加了一个循环,但它只会一遍又一遍地循环最后一句话:p

您必须重置要发送到 typewrite 函数的数组中的字符串。这在其他解决方案中没有发生。

于 2013-09-28T22:44:01.703 回答
2

把你的代码放在一个区间是一个好主意。但是,您必须在再次调用之前检查代码执行是否完成。您可以使用用作标志的布尔变量来执行此操作。

var timerId = setInterval(function() {
    flag=true;
 if( flag){
     flag=false;
    $("div").typed({
     strings: ["First sentence.", "Second sentence."],
     typeSpeed: 30, // typing speed
     backDelay: 500, // pause before backspacing
     callback: function () {
         flag=true;
         //destroy typed plugin here. See in the api if 
         //there is a method for doing that.

     }
 });

 }

}, 1000);

当您不想停止循环时,您可以简单地销毁计时器

clearInterval(timerId);

编辑:我在插件中添加了一个选项

$("#typed").typed({
    strings: ["Typed.js is a jQuery plugin.", "It types out sentences."],
    typeSpeed: 30,
    backDelay: 500,
    loop:true,
    callback: function(){ alert('end'); }
});

编辑的插件文件

!function($){

    "use strict";

    var Typed = function(el, options){

        // for variable scope's sake
        self = this;

        // chosen element to manipulate text
        self.el = $(el);
        // options
        self.options = $.extend({}, $.fn.typed.defaults, options);

        // text content of element
        self.text = self.el.text();

        // typing speed
        self.typeSpeed = self.options.typeSpeed;

        // typing speed
        self.loop = self.options.loop;

        // amount of time to wait before backspacing
        self.backDelay = self.options.backDelay;

        // input strings of text
        self.strings = self.options.strings;

        // character number position of current string
        self.strPos = 0;

        // current array position
        self.arrayPos = 0;

        // current string based on current values[] array position 
        self.string = self.strings[self.arrayPos];

        // number to stop backspacing on.
        // default 0, can change depending on how many chars
        // you want to remove at the time
        self.stopNum = 0;

        // number in which to stop going through array
        // set to strings[] array (length - 1) to stop deleting after last string is typed
        self.stopArray = self.strings.length-1;

        // All systems go!
        self.init();
    }

        Typed.prototype =  {

            constructor: Typed

            , init: function(){
                // begin the loop w/ first current string (global self.string)
                // current string will be passed as an argument each time after this
                self.typewrite(self.string, self.strPos);
                self.el.after("<span id=\"typed-cursor\">|</span>");
            }

            // pass current string state to each function
            , typewrite: function(curString, curStrPos){

                // varying values for setTimeout during typing
                // can't be global since number changes each time loop is executed
                var humanize = Math.round(Math.random() * (100 - 30)) + self.typeSpeed;

                // ------ optional ------ //
                // custom backspace delay
                // if (self.arrayPos == 1){
                //  self.backDelay = 50;
                // }
                // else{ self.backDelay = 500; }

                // containg entire typing function in a timeout
                setTimeout(function() {

                    // make sure array position is less than array length
                    if (self.arrayPos < self.strings.length){

                        // start typing each new char into existing string
                        // curString is function arg
                        self.el.text(self.text + curString.substr(0, curStrPos));

                        // check if current character number is the string's length
                        // and if the current array position is less than the stopping point
                        // if so, backspace after backDelay setting
                        if (curStrPos > curString.length && self.arrayPos < self.stopArray){
                            clearTimeout();
                            setTimeout(function(){
                                self.backspace(curString, curStrPos);
                            }, self.backDelay);
                        }

                        // else, keep typing
                        else{
                            // add characters one by one
                            curStrPos++;
                            // loop the function
                            self.typewrite(curString, curStrPos);
                            // if the array position is at the stopping position
                            // finish code, on to next task
                            if (self.arrayPos == self.stopArray && curStrPos == curString.length){
                                // animation that occurs on the last typed string
                                // place any finishing code here

                                if(self.loop){
                                        self.arrayPos=0;
                                        curStrPos=0;
                                }else{
                                self.options.callback();
                                clearTimeout();}
                            }
                        }
                    }

                // humanized value for typing
                }, humanize);

            }

            , backspace: function(curString, curStrPos){

                // varying values for setTimeout during typing
                // can't be global since number changes each time loop is executed
                var humanize = Math.round(Math.random() * (100 - 30)) + self.typeSpeed;

                setTimeout(function() {

                        // ----- this part is optional ----- //
                        // check string array position
                        // on the first string, only delete one word
                        // the stopNum actually represents the amount of chars to
                        // keep in the current string. In my case it's 14.
                        // if (self.arrayPos == 1){
                        //  self.stopNum = 14;
                        // }
                        //every other time, delete the whole typed string
                        // else{
                        //  self.stopNum = 0;
                        // }

                    // ----- continue important stuff ----- //
                        // replace text with current text + typed characters
                        self.el.text(self.text + curString.substr(0, curStrPos));

                        // if the number (id of character in current string) is 
                        // less than the stop number, keep going
                        if (curStrPos > self.stopNum){
                            // subtract characters one by one
                            curStrPos--;
                            // loop the function
                            self.backspace(curString, curStrPos);
                        }
                        // if the stop number has been reached, increase 
                        // array position to next string
                        else if (curStrPos <= self.stopNum){
                            clearTimeout();
                            self.arrayPos = self.arrayPos+1;
                            // must pass new array position in this instance
                            // instead of using global arrayPos
                            self.typewrite(self.strings[self.arrayPos], curStrPos);
                        }

                // humanized value for typing
                }, humanize);   

            }

        }

    $.fn.typed = function (option) {
        return this.each(function () {
          var $this = $(this)
            , data = $this.data('typed')
            , options = typeof option == 'object' && option
          if (!data) $this.data('typed', (data = new Typed(this, options)))
          if (typeof option == 'string') data[option]()
        });
    }

    $.fn.typed.defaults = {
        strings: ["These are the default values...", "You know what you should do?", "Use your own!", "Have a great day!"],
        // typing and backspacing speed
        typeSpeed: 0,
        // time before backspacing
        backDelay: 500,
        loop:false,
        // ending callback function
        callback: function(){ null }
    }


}(window.jQuery);
于 2013-09-28T21:16:16.727 回答
2
$(func = function () {
      $(".element").typed({
          strings: ['Statement One', 
            'Satemante Two',
          'Statemante Three', ''],
          typeSpeed: 40, // typing speed
          backDelay: 2000,
          callback: function (){
            func();
          }
      });

  });
于 2016-07-26T18:47:03.763 回答
1

尝试这个:

$(".typed-text").typed({
    strings: ["test 1", "test 2", "test 3", "test 4"],
    typeSpeed: 10,
    backDelay: 1500,
    loop: !0, // here
    startDelay: 1000,
});
于 2016-10-19T09:31:04.217 回答
0

你有没有尝试过:

var myFunc = function() {
  $("span.text-change").typed({
     strings: ["First sentence.", "Second sentence."],
     typeSpeed: 30, // typing speed
     backDelay: 500, // pause before backspacing
     callback: function () {
         myFunc();
     }
  });
}
于 2013-09-28T20:53:48.443 回答
0

只需添加属性循环:1。用于无限循环

$(".typed-text").typed({ strings: ["test 1", "test 2", "test 3", "test 4"], typeSpeed: 10, backDelay: 1500, loop: 1, / / 这里 });

于 2017-08-11T09:48:32.450 回答