在脚本中执行另一个脚本中的函数

发布于 2024-12-10 22:24:44 字数 1389 浏览 0 评论 0原文

我的页面正文中运行了两个脚本。当按下键盘上的“a”时,另一个脚本就会运行。如何添加一些延迟,然后再次触发第一个脚本?我已经尝试使用下面的代码,它不起作用。最好,我也想取消第二个脚本开头的第一次超时。

 <script id="source" language="javascript" type="text/javascript">
$(function (){
  function reloading(){
      $.ajax({                                      
      url: 'api.php',                    
      data: "",                      
      dataType: 'json',             
      success: function(data)        
      {
        var id = data[0];             
          _id = id;
        var vname = data[1];           
        var message = data[2]; 
        var timestamp = data[3]; 
        var field1 = data[4]; 
        _field1 = field1;
        var val2 = parseInt(field1, 10) ;
        _val2 = val2;
        $('#output').hide().html( message ).fadeIn("slow");   
         $('#username').hide().html( vname +":" ).fadeIn("slow");
      setTimeout(function(){
      reloading();
    }, 60000);

      }
      });
  }
  reloading();
}); 
  </script>

  <script>
  $(document).jkey('a',function() {
     $.post("update.php", { "id": _id} )
      $('#output').hide().html( "<i>Message</i><br> <br>" +_val2 +" additional." ).fadeIn("slow");
      $('#username').fadeOut("fast");
      $('#valg1').fadeOut("fast");
      $('#valg2').fadeOut("fast");
      });
     setTimeout("reloading()",1000); 
</script>

I have two scripts running in the body of my page. When "a" is pressed on the keyboard, another script runs. How can I add some delay and then trigger the first script again? I have tried with the code below, it does not work. Prefferably, I would like to cancel the first timeout in the beginning of the second script as well.

 <script id="source" language="javascript" type="text/javascript">
$(function (){
  function reloading(){
      $.ajax({                                      
      url: 'api.php',                    
      data: "",                      
      dataType: 'json',             
      success: function(data)        
      {
        var id = data[0];             
          _id = id;
        var vname = data[1];           
        var message = data[2]; 
        var timestamp = data[3]; 
        var field1 = data[4]; 
        _field1 = field1;
        var val2 = parseInt(field1, 10) ;
        _val2 = val2;
        $('#output').hide().html( message ).fadeIn("slow");   
         $('#username').hide().html( vname +":" ).fadeIn("slow");
      setTimeout(function(){
      reloading();
    }, 60000);

      }
      });
  }
  reloading();
}); 
  </script>

  <script>
  $(document).jkey('a',function() {
     $.post("update.php", { "id": _id} )
      $('#output').hide().html( "<i>Message</i><br> <br>" +_val2 +" additional." ).fadeIn("slow");
      $('#username').fadeOut("fast");
      $('#valg1').fadeOut("fast");
      $('#valg2').fadeOut("fast");
      });
     setTimeout("reloading()",1000); 
</script>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

最美的太阳 2024-12-17 22:24:44

您必须将 reloaded() 放在全局范围内才能从另一个脚本访问它。现在它位于一个匿名函数内(有点黑客来减轻全局范围污染),但对于您的情况,如果将两个脚本合并为一个不可行,您将不得不污染全局范围。

如果它是一次性函数,请给它一个唯一的前缀或其他东西:

myApp_reloaded();

如果您有一堆需要位于全局范围内的函数,请创建一个包装器对象。您可以使用许多不同的模式,我更喜欢这个......

    function MyApp() {

    }
    MyApp.prototype.reloaded = function () {
     // reload func body here
    }




var myApp = new MyApp();

现在您可以全局访问这些方法:

myApp.reloaded();

You have to put reloaded() in global scope to access it from another script. Right now it is inside an anonymous function (a bit of a hack to mitigate global scope pollution), but for your case if it isn't feasible to merge the two scripts into one, you are going to have to pollute global scope.

If it is a one-off function, give it a unique prefix or something:

myApp_reloaded();

If you have a bunch of functions that need to be in the global scope, create a wrapper object. There are a bunch of different patterns you can use, I prefer this...

    function MyApp() {

    }
    MyApp.prototype.reloaded = function () {
     // reload func body here
    }




var myApp = new MyApp();

now you can access the methods globally:

myApp.reloaded();
始终不够 2024-12-17 22:24:44

快速破解,直到有人提出一种不会污染全局范围的更优雅的方法

<script id="source" language="javascript" type="text/javascript">
var tId;
function reloading(){
  $.ajax({                                      
    url: 'api.php',                    
    data: "",                      
    dataType: 'json',             
    success: function(data) {
      var id = data[0];             
      _id = id;
      var vname = data[1];           
      var message = data[2]; 
      var timestamp = data[3]; 
      var field1 = data[4]; 
      _field1 = field1;
      var val2 = parseInt(field1, 10) ;
      _val2 = val2;
      $('#output').hide().html( message ).fadeIn("slow");   
      $('#username').hide().html( vname +":" ).fadeIn("slow");
      clearTimeout(tId);
      tId=setTimeout(function(){ reloading();}, 60000);
    }
  });
}

$(function (){
  reloading();
}); 

$(document).jkey('a',function() {
  $.post("update.php", { "id": _id} )
  $('#output').hide().html( "<i>Message</i><br> <br>" +_val2 +" additional." ).fadeIn("slow");
  $('#username').fadeOut("fast");
  $('#valg1').fadeOut("fast");
  $('#valg2').fadeOut("fast");
  clearTimeout(tId);
  tId = setTimeout(function() { reloading()},1000); 

});
</script>

Quick hack until someone comes with a more elegant one that does not pollute the global scope

<script id="source" language="javascript" type="text/javascript">
var tId;
function reloading(){
  $.ajax({                                      
    url: 'api.php',                    
    data: "",                      
    dataType: 'json',             
    success: function(data) {
      var id = data[0];             
      _id = id;
      var vname = data[1];           
      var message = data[2]; 
      var timestamp = data[3]; 
      var field1 = data[4]; 
      _field1 = field1;
      var val2 = parseInt(field1, 10) ;
      _val2 = val2;
      $('#output').hide().html( message ).fadeIn("slow");   
      $('#username').hide().html( vname +":" ).fadeIn("slow");
      clearTimeout(tId);
      tId=setTimeout(function(){ reloading();}, 60000);
    }
  });
}

$(function (){
  reloading();
}); 

$(document).jkey('a',function() {
  $.post("update.php", { "id": _id} )
  $('#output').hide().html( "<i>Message</i><br> <br>" +_val2 +" additional." ).fadeIn("slow");
  $('#username').fadeOut("fast");
  $('#valg1').fadeOut("fast");
  $('#valg2').fadeOut("fast");
  clearTimeout(tId);
  tId = setTimeout(function() { reloading()},1000); 

});
</script>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文