Javascript Callback function malfunction

Zac Jacob

I am trying to implement an anonymous callback function. The code here I wrote is janky I know.

Typing intro is a span, it activates the .typed animation with the given parameters. What I need to activate, and what isn't, is the last function to remove the two elements by id there. Can't figure out how.

<!-- Executes intro typing text -->
<script>
  document.getElementById('intro-button').onclick = function() {
    $(function() {
      $("#typing-intro").typed({
             strings: ["&emsp;INITSEG  = DEF_INITSEG</br>&emsp;SYSSEG"],
             typeSpeed: 0,
             backDelay: 20,
             loop: false,
             loopCount: false,
        }, function (){
           document.getElementById("intro-section").remove();
           document.getElementById("typing-intro").remove();
        });
      });
    }
    ///script
</script>
Alex

As MinusFour points out, the typed animation function doesn't take a callback function as the final parameter. Instead a "callback" function is passed to it through the options

Beyond the question, there's a lot you can clean up with the code. Since you're using jQuery already, you can clean up the event handler. Instead of document.getElementById('intro-button').onclick = function() { ... } you can have $('#intro-button').click(function() { ... });. If you are placing this <script> in the <head>, you will want to wrap the entire thing in in $(function() { ... }), not have in stuck in the middle of your code.

Finally, since you're using jQuery everywhere else, instead of document.getElementById("intro-section").remove(); just have $('#intro-section').remove().

So it would like this:

$(function() {
    $('#intro-button').click(function() {
          $("#typing-intro").typed({
             strings: ["&emsp;INITSEG  = DEF_INITSEG</br>&emsp;SYSSEG"],
             typeSpeed: 0,
             backDelay: 20,
             loop: false,
             loopCount: false,
             callback: function (){
                $("#intro-section").remove();
                $("#typing-intro").remove();
             }
        });
    })
})

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

JavaScript Multiple Callback Function

From Dev

Javascript array with callback function

From Dev

Usage of Javascript callback function

From Dev

Javascript Function with Callback and Parameters

From Dev

javascript callback on function

From Dev

JavaScript: custom callBack function

From Dev

Javascript with callback function not working

From Dev

OOP with Javascript and callback function

From Dev

Javascript callback function not work

From Dev

Creating a callback on javascript function

From Dev

Javascript anonymous callback function

From Dev

javascript callback function selection

From Dev

Callback with arrow function in javascript

From Dev

Javascript Custom Callback Function

From Dev

javascript callback function on a separate thread

From Dev

javascript callback function in chrome extension

From Dev

Javascript callback function for confirm bootbox

From Dev

callback function in javascript replace() is not called

From Dev

object callback function is undefined Javascript

From Dev

Javascript function callback dependant on timeout

From Dev

javascript callback function in chrome extension

From Dev

callback function argument in javascript / jQuery

From Dev

JavaScript: breaking out of a callback function

From Dev

Javascript function callback variable scope

From Dev

Javascript callback function in ajax request

From Dev

Javascript callback function firing twice

From Dev

Callback function with a truthy value in JavaScript

From Dev

Ajax call, function callback, javascript

From Dev

Javascript callback function not being fired