嵌套返回NaN的函数

时间:2016-08-03 13:05:04

标签: javascript function counter calculator

当我尝试在现有函数中嵌套一个简单的计数器函数时,我遇到了错误。我知道有些人对此不以为然,所以如果还有其他办法,我们将不胜感激。基本上,我调用一个函数来解析JSON字符串以获得3个数字。每次进行新的计算时,我想用计数器为这3个数字设置动画。我的直觉是将这个计数器函数置于同一个for循环中,以便自己获取数字。这样做会返回NaN。我相信我在这里失踪的东西很少,我希望有人可以接受。

代码:

var text = '{JSON data}'
var obj = JSON.parse(text);

function calculate() {
    var e = document.getElementById("ltSpecialtyList");
    var selectedSpec = e.options[e.selectedIndex].text;
    console.log(selectedSpec);

    var x = document.getElementById("ltLocationList");
    var selectedState = x.options[x.selectedIndex].text;
    console.log(selectedState);

    for (i = 0; i < obj.values.length; i++)
    {
        if (obj.values[i].State == selectedState && obj.values[i].Specialty == selectedSpec)
        {
            // DISPLAY RESULTS DIV (JQUERY)
            var $resultsDiv = $("#ROI-results");
            $resultsDiv.fadeIn();

            // CAPTURE HEADER SPAN NAMES
            var headerState = document.getElementById("header-state-name");
            var headerSpec = document.getElementById("header-specialty-name");
            // CLEAR STATE AND SPECIALTY FROM SPAN
            headerState.innerHTML = "";
            headerSpec.innerHTML = "";
            // ADD SELECTED STATE AND SPECIALTY
            headerState.innerHTML += selectedState;
            headerSpec.innerHTML += selectedSpec;

            // CAPTURE RESULT DATA DIVS
            var permResults = document.getElementById("ROI-results-perm-data");
            var locumResults = document.getElementById("ROI-results-locums-data");
            var uncollectedResults = document.getElementById("ROI-results-uncollected-data");
            // CLEAR DATA DIVS
            permResults.innerHTML = "";
            locumResults.innerHTML = "";
            uncollectedResults.innerHTML = "";
            //POPULATE DIV WITH DATA
            permResults.innerHTML += "<p class='ROI-results-data-number univers-bold'>" + obj.values[i].Permanent + "</p>";
            locumResults.innerHTML += "<p class='ROI-results-data-number univers-bold'>" + obj.values[i].Locums + "</p>";
            uncollectedResults.innerHTML += "<p class='ROI-results-data-number univers-bold'>" + obj.values[i].Gross + "</p>";
        }

        // COUNTER
        $('.ROI-results-data-number').each(function () {
            $(this).prop('Counter',0).animate({
                Counter: $(this).text()
            }, {
                duration: 4000,
                easing: 'swing',
                step: function (now) {
                    $(this).text(Math.ceil(now));
                }
            });
        });
    } 
}

0 个答案:

没有答案