English 中文(简体)
标本代码,没有警报就失效,否则就完美可行
原标题:Javascript code that doesn t work without an alert before it, otherwise it works perfectly

在问一个问题之前,我曾经尝试过认真研究这个问题,但看起来我身陷困境,想看看是否有人知道这个问题的明确答案。 我见过一两个类似的问题,但他们似乎没有指出我的问题,或者至少我不明白他们要做什么。

我正在研究一个 RPG 系统音频 PHP 功能, 该功能在被调用时播放音乐, 如果用户没有哑音的音乐选项, 它会使用音频标签 。 (在函数 $song 中, 歌曲被投到第一个参数中 。) 如果此函数被调回真实状态, 那么它会使用一些 Javascript 。 请原谅代码, 因为它有点乱( 对我来说), 但这里使用以下代码, 包括 javascrip 代码 :

function playmusic($song, $resume=false, $loop=true)
 {
  global $baseurl, $loggeduser;

 if ($resume == true)
  {
   $buttons = "
    <button onclick="resetplaytime()" type="button">Reset</button>
    <button onclick="alertplaytime()" type="button">Alert Time</button>";
   $resume = "
    <script type="text/javascript">
      var curmusic_$song = document.getElementById("music_$song");
      var musictime = getcookie("musicplaytime_$song");

     if (musictime != null && musictime != "")
      {
       alert(musictime);
       curmusic_$song.currentTime = musictime; 
      }

      window.onload=settimecookie();

     function getcookie(c_name)
      {
       var i,x,y,ARRcookies=document.cookie.split(";");
      for (i=0;i<ARRcookies.length;i++)
       {
        x=ARRcookies[i].substr(0,ARRcookies[i].indexOf("="));
        y=ARRcookies[i].substr(ARRcookies[i].indexOf("=")+1);
        x=x.replace(/^s+|s+$/g,"");
       if (x==c_name)
        {
         return unescape(y);
        }
       }
      }

     function settimecookie()
      {
       var playtime = curmusic_$song.currentTime;
      if (playtime != null && playtime != "")
       {
        document.cookie =  musicplaytime_$song=  + playtime;
       }

       setTimeout("settimecookie()", 50);
      }

     function alertplaytime()
      {
       alert(musictime);
      }

     function resetplaytime()
      {
       curmusic_$song.currentTime=0;
      }

    </script>";
  }

 if ($loop == true)
  {
   $songloop = " loop="true"";
  }

 if ($loggeduser[ mutemusic ] == 0)
  {
   return "
    </table>
    $buttons
    <audio id="music_$song" autoplay="true"$songloop preload="true">
      <source src="$baseurl/boardfiles/effects/$song.ogg" type="audio/ogg" />
      <source src="$baseurl/boardfiles/effects/$song.mp3" type="audio/mp3" />
    </audio>
    $resume
    <table class="brdr" cellpadding="0">";
  }
 }

现在,如果我把将音频标签播放时间设定为 cookie 值的循环的提醒从循环中移除,整个脚本就不管用,除非您使用按钮直接调用一个函数,然后该函数就起作用。

我尝试了几件事情来把这个函数变成一个函数,然后直接调用它(这似乎只是与点击事件有关,但如果你期望它会运行在加载中或自动事件之后,它就会失败。),然后在函数的后半部分移动它(加上设定时间cookie()的加载,因为它会将饼干设置到当前播放时间),而现在我完全没有想法了。

I am hoping to get an answer for this as I am sure this code could help others who ve been trying to get some sort of resume audio function that isn t bulky or confusing to use. :) I ve been trying to find a solution for quite awhile and never have been this close. Thank you for reading. If you d like the whole PHP function then I ll show that too but there s really nothing to see except two debug buttons (Reset and Alert cookie) and the audio tag with the id music_$song .

<强度 > 编辑 : 仅要明确, 在代码中, 在双引号之前有一些逃跑, 因为正返回 pHP 函数中的 Javascript 。

编辑 2: 现在用粗体粗体显示这个编辑, 因为有人错过了它。 此外, 我添加了 PHP 部分 。




Last edit: I have now seemed to figure something out that got me closer to what I wish to achieve. I changed the above code to the following and it works flawless for me.

function playmusic($song, $resume=false, $loop=true)
 {
  global $baseurl, $loggeduser;

 if ($resume == true)
  {
   $resume = "
    <!--
    <button onclick="resetplaytime();" type="button">Reset</button>
    <button onclick="alertplaytime();" type="button">Alert Time</button>
    <button onclick="settimecookie();" type="button">Start Cookie tracking</button>
    -->

    <script type="text/javascript">
     function getcookie(c_name)
      {
       var i,x,y,ARRcookies=document.cookie.split(";");
      for (i=0;i<ARRcookies.length;i++)
       {
        x=ARRcookies[i].substr(0,ARRcookies[i].indexOf("="));
        y=ARRcookies[i].substr(ARRcookies[i].indexOf("=")+1);
        x=x.replace(/^s+|s+$/g,"");
       if (x==c_name)
        {
         return unescape(y);
        }
       }
      }

     function settimecookie()
      {
       var curmusic_$song = document.getElementById("music_$song");
       var playtime = curmusic_$song.currentTime;
      if (playtime != null && playtime != "")
       {
        document.cookie = "musicplaytime_$song=" + playtime;
       }

       setTimeout("settimecookie()", 50);
      }

     function resumeplaytime()
      {
       var curmusic_$song = document.getElementById("music_$song");
       var musictime = getcookie("musicplaytime_$song");
      if (musictime != null && musictime != "")
       {
        curmusic_$song.currentTime = musictime; 
       }
       curmusic_$song.play(); 

       settimecookie();
      }

      window.onload=resumeplaytime;

     function alertplaytime()
      {
       var musictime = getcookie("musicplaytime_$song");
       alert(musictime);
      }

     function resetplaytime()
      {
       var curmusic_$song = document.getElementById("music_$song");
       curmusic_$song.currentTime=0;
      }

    </script>";
  }
 else
  {
   $autoplay = "autoplay="true"";
  }

 if ($loop == true)
  {
   $songloop = " loop="true"";
  }

 if ($loggeduser[ mutemusic ] == 0)
  {
   return "
    </table>
    $resume
    <audio id="music_$song" $autoplay$songloop preload="true">
      <source src="$baseurl/boardfiles/effects/$song.ogg" type="audio/ogg" />
      <source src="$baseurl/boardfiles/effects/$song.mp3" type="audio/mp3" />
    </audio>
    <table class="brdr" cellpadding="0">";
  }
 }



It seems to work perfectly now. I changed a few things and now works as I want it to. Thanks for the help! :)

最佳回答

看起来您试图在 DOM 可用之前执行 < code> document. getEplementById ("mus_ $song") 。 请为此使用一个加载处理器 。

然而,我找不到任何呼叫 alert () <() wardplaytime () <() 的电话来等待这个时间 - 特别是没有象你所描述的那样进入循环。

此外,行 window.onload=settimecookie (); is buggy. settimecookie 执行时不会返回事件处理器函数 。

问题回答

暂无回答




相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签