Cesar Bielich
Cesar Bielich

Reputation: 4945

how to pass a string into a nested function in jquery

I am trying to figure out why I cant pass a string into a function that contains another function. I get undefined when I alert.

$.fn.downCount = function (current_time) {
    function countdown(current_time) {
        alert(current_time)
    }
})

var current_time = "01:00";
downCount(current_time);

Upvotes: 0

Views: 25

Answers (1)

Matthew Johnson
Matthew Johnson

Reputation: 5155

You never actually call the inner function. Call the function and pass in the current_time.

$.fn.downCount = function (current_time) {
    function countdown() {
        alert(current_time)
    }
    countdown();
}


var current_time = "01:00";
$.fn.downCount(current_time);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
...

Also, as Andrew mentioned, you don't need to pass in the current_time into the countdown function. It can be simplified to:

$.fn.downCount = function (current_time) {
    function countdown() {
        alert(current_time)
    }
    countdown();
}

Upvotes: 1

Related Questions