1

I have the following js files and I want to access one function from another function but I call it in another js file. I receive this error when I call it Uncaught ReferenceError: vidPause is not defined.

(function($) {
    $.fn.controlls = function(opt) {
    ......
    function init() {
    .....
    }
    function vidPause(e){
       e.stopPropagation();
       $.fn.controlls.toggleBarPlayingButtons('pause');
       video.pause();
    }
    .....

From this js file I want to call vidPause in the following function in another js file

function myFunction(e) {
    if (video) {
        $('video').controlls(vidPause());
Markus Hayner
  • 2,869
  • 2
  • 28
  • 60

1 Answers1

0

This issue is scope.

init() and vidPause() are private to the (function($) { call. They will not be directly accessible the way you are trying to access them.

Many jquery plugins use text (not my preference, but that's how they work), eg $.dialog("open"), so you could do something like that (not sure if opt is meant to mean action, so update accordingly) :

(function($) {
    $.fn.controlls = function(action, opt) {
        switch (action) {
            case "pause":
                vidPause(opt);
                break;
            ...

usage

$('video').controlls("pause");

It might be possible to add the methods as if using namespaces, but I've not seen this in plugins so depends on what you are doing with your plugin (ie releasing it) whether you want to be consistent, eg:

(function($) { 
    $.fn.controlls = {};
    $.fn.controlls.pause = function(opt) {
        video.pause();
        ...

usage

$('video').controlls.pause()
freedomn-m
  • 27,664
  • 8
  • 35
  • 57