【问题标题】:Javascript how to make functions/variables globally availableJavascript如何使函数/变量全局可用
【发布时间】:2023-04-11 01:33:02
【问题描述】:

我是一个初学者并制作了一个自定义的 SoundCloud Player,但 javascript 不是很好,因为我无法获取全局变量和函数。目前,我在某些函数中有与另一个函数相同的代码来计算一个变量,比如播放器中音乐的当前时间。特别是对于currenttime,我有 2 个计时器正在运行,我还需要一个。解决此问题的一种简单方法是使当前秒等变量全局可用。

我尝试将所有代码放入document.ready 或使用返回值,但没有成功。

这是我的代码:

function toggle() {
    var widget = SC.Widget(document.getElementById('soundcloud_widget'));
    widget.isPaused(function(boolean){
        widget.toggle();
        playing = boolean;
        if(playing == true)
        {
            playtoggle.className ="";
            playtoggle.className ="play";
        }
        else if(playing == false)
        {
            playtoggle.className ="";
            playtoggle.className ="pause";
        }
    });
}
function refresher()
{
    setInterval(function(){timer()},10);

    var widget = SC.Widget(document.getElementById('soundcloud_widget'));

    widget.getDuration(function(duration){
        endtime = Math.round(duration/1000);
        var minute = Math.floor((endtime / 100) * 100 / 60);
        var sekunde = Math.round(endtime  - (minute * 60));
        if(sekunde<10)
        {
            document.getElementById('endtime').innerHTML = " "+ minute + ":0" + sekunde;
        }
        else
        {
            document.getElementById('endtime').innerHTML = " "+ minute + ":" + sekunde;
        }
    });  

}
function timer() {
    var widget = SC.Widget(document.getElementById('soundcloud_widget'));

    widget.getCurrentSound(function(music){
    document.getElementById('title').innerHTML = music.title;
    })  

    widget.getPosition(function(position){
        currenttime = Math.round(position/1000);
        currentminute = Math.floor((currenttime /100) * 100 / 60);
        currentsek = Math.round(currenttime - (currentminute * 60));
        if(currentsek < 10)
        {
            document.getElementById('time').innerHTML = currentminute + ":0" + currentsek;
        }
        else
        {
            document.getElementById('time').innerHTML = currentminute + ":" + currentsek;
        }

        widget.getDuration(function(duration){
            endtime = Math.round(duration/1000);
            minute = Math.floor((endtime / 100) * 100 / 60);
            sekunde = Math.round(endtime  - (minute * 60));

            var pWidth = $('#gutter').width();
            var seekToposition = (position / duration)*pWidth;
            $('#seekTo').css('left', seekToposition-15+"px");
            $('#progress').css('width', seekToposition+"px");
        });

        if(currentminute == minute && currentsek == sekunde)
        {
            playtoggle.className="";
            playtoggle.className="pause";
        }
    });
}
function seekTo() {
    var widget = SC.Widget(document.getElementById('soundcloud_widget'));

    var pWidth = $('#gutter').width();
    $('#gutter').click(function(e){
        var scrub = (e.pageX);
        $('#seekTo').css('left', scrub+"px");
        $('#progress').css('width', scrub+"px");
        widget.seekTo((scrub)/pWidth*311498);
    });
}
function keyfunction(e){
    var widget = SC.Widget(document.getElementById('soundcloud_widget'));
    switch(e.keyCode){
      case 32:
        widget.isPaused(function(boolean){
            widget.toggle();
            playing = boolean;
            if(playing == true)
            {
                playtoggle.className ="";
                playtoggle.className ="play";
            }
            else if(playing == false)
            {
                playtoggle.className ="";
                playtoggle.className ="pause";
            }
        });
      break;
    }
}

JSFiddle

它在我的计算机上运行,​​但我想通过将变量设为全局来保存一些代码。

【问题讨论】:

  • 它仅用于测试。最后会有另一首音乐。经过数小时的测试,这音乐很烂:P

标签: javascript jquery global-variables soundcloud document-ready


【解决方案1】:

在 Javascript 中,变量总是有一个作用域,因此理论上不存在诸如“全局”变量(例如其他语言中的 static)。

但您可以在 window 对象(或代码的根级别)上定义任何内容,任何地方都可以访问。

例如

window.myVariable = 4;
function alertMyVariable() {
  alert(window.myVariable); // alerts 4;
}

或以下具有相同语义:

var myVariable = 4;
function alertMyVariable() {
  alert(myVariable); // alerts 4;
}

但请注意,对于后者,您可能会使用本地变量覆盖该变量:

var myVariable = 4;
function alertMyVariable() {
  var myVariable = 6;
  alert(myVariable); // alerts 6!;
}

所以最好用window 来限定它。

这是一个DEMO fiddle

【讨论】:

  • 感谢您的回答,有趣。不知道。我希望这会有所帮助。
【解决方案2】:
var myvariable;

function myFunction() {
  myvariable = 1;
}



function myFunction2() {
  if (myvariable == 1)
    // something
}

【讨论】:

    猜你喜欢
    • 2023-03-25
    • 1970-01-01
    • 2020-06-12
    • 1970-01-01
    • 2023-01-29
    • 2011-06-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多