Listen for function after other function is finished

I am loading in html with a RS-JAX GET request. After the request, I need function two to start listening for input.

Below is what I tried, however now I am getting two errors: 1) TypeError: functionTwo is not a function. 2) ReferenceError: loadIngredients is not defined.

My guess for #2 is that I'm calling it in another function and the var functionOne is messing with it. I wouldn't know about #1.

Is there a way to fix this? Maybe by editing the code, maybe by using another way to listen for when function one is done?

Function one

var functionOne = function loadIngredients(selected) {  
    var r = $.Deferred();
    var username = window.sessionStorage.getItem("huidigeGebruiker");
    var datum = document.getElementById("datepicker").value;
    var url = "restservices/ingredients?Q1=" + username + "&Q2=" + datum;
        $.ajax({
            url : url,
            method : "GET",
            beforeSend : function(xhr) {
                var token = window.sessionStorage.getItem("sessionToken");
                xhr.setRequestHeader('Authorization', 'Bearer ' + token);
            },
            success : function(data) {
                $(".table").find("tr:gt(0):not(:last)").remove();
                 $(data).each(function (index) {
                     $(".table").find('tr:last').prev().after('<tr><td class="ingredient">'+this.ingredientnaam+'</td><td class="hoeveelheid"><input class="gramtxt" type="text" value="'+this.hoeveelheid+'" name="gramtxt" id="gramtxt"></td><td class="subtotaal">'+this[selected]+'</td><td class="removeingredient"><a href="#"> <i class="fa fa-times text-red"></i></a></td></tr>');
                    });
                 loadTotals();
                 ingredienten = [];
                 loadAllIngredients();
            },
            error: function(xhr){
                $(".dagboektable").html('Ophalen ingrediënten mislukt. Ben je ingelogd?');
            },
        });
        return r;
}

Function two

var functionTwo = $('#gramtext').bind('input', function() { 
    console.log("Y");
    var hoeveelheid = $(this).val();
    console.log(hoeveelheid);
});

Listener

functionOne().done( functionTwo() );

Things needs to be changed :

First :

var functionOne = function loadIngredients(selected) { 
To
var functionOne = function (selected) { 

Second :

$.ajax(......); return r;
TO
return $.ajax(......);

Third :

var functionTwo = $('#gramtext').bind('input', function() { 
    console.log("Y");
    var hoeveelheid = $(this).val();
    console.log(hoeveelheid);
});

TO

var functionTwo = function(){
 $('#gramtext').bind('input', function() { 
    console.log("Y");
    var hoeveelheid = $(this).val();
    console.log(hoeveelheid);
 });
}

This will solve almost all the erros and then try again.

链接地址: http://www.djcxy.com/p/358.html

上一篇: 检查一个目录是否存在于一个shell脚本中

下一篇: 在其他功能完成后收听功能