首页 > 解决方案 > 跳过按钮 - 如何用另一个功能停止一个功能

问题描述

我有两个功能。第一个功能是使用 typed.js 在 div (.con) 中写入一些文本。然后我有一个跳过按钮和另一个功能,它应该停止输入并只显示文本。

HTML:

<button onclick="skip()">skip</button>

Javascript:

function typing() {
    $(.con).typed({
        strings: ["text"],
        stringsElement: null,
        typeSpeed: 30,
        startDelay: 400,
        showCursor: false,
        cursorChar: "|",
        attr: null,
        contentType: 'html',
      });
}

function skip() {
    $(".con").html("TEXT");
}

问题是即使它显示了文本,它也会消失,因为 typed.js 仍在编写文本。我不知道用这个功能停止以前的功能。

标签: javascriptfunction

解决方案


更改您的代码以调用这样的键入停止方法,以防止它继续放入文本并允许您设置它:

function typing() {
    window.conTyped = $(".con").typed({
        strings: ["text"],
        stringsElement: null,
        typeSpeed: 30,
        startDelay: 400,
        showCursor: false,
        cursorChar: "|",
        attr: null,
        contentType: 'html',
      });
}

function skip() {
    window.conTyped.stop();
    $(".con").html("TEXT");
}

推荐阅读