0

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);
1
  • But that code doesn't alert anything. Anyways, remove current_time as an argument of countdown if you want to use the current_time from downCount. Commented Sep 1, 2017 at 20:19

1 Answer 1

1

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();
}
Sign up to request clarification or add additional context in comments.

1 Comment

Beat me to it. I'd also like to add that in OP's original code there's an unnecessary ")" after the closing curly brace on initial plugin definition. Looks like you caught that as well. Cheers.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.