【问题标题】:Animated random number not running动画随机数未运行
【发布时间】:2013-03-22 13:36:06
【问题描述】:

我使用了类似 post in 的代码 Random Number Effect In Jquery.. 结果http://jsfiddle.net/ZDsMa/94/...

现在我在一个文件 php(index.php) 中编写完整的代码 html 和 jquery 并放在我的服务器上...

<html>
<title>Randomize Number</title>
<head>
<style type="text/css">
#output {
    margin: 20px;
    padding: 20px;
    background: gray;
    border-radius: 10px;
    font-size: 80px;
    width: 160px;
    color: red;
}
</style>

<script type="text/javascript" src="http://code.jquery.com/jquery-latest.js"></script>
<script>
var output, started, duration, desired;
// Constants
duration = 5000;
desired = '50';

// Initial setup
output = $('#output');
started = new Date().getTime();

// Animate!
animationTimer = setInterval(function() {
    // If the value is what we want, stop animating
    // or if the duration has been exceeded, stop animating
    if (output.text().trim() === desired || new Date().getTime() - started > duration) {
        console.log('animating');
        // Generate a random string to use for the next animation step
        output.text('' + Math.floor(Math.random() * 990)

        );

    } else {
        console.log('animating');
        // Generate a random string to use for the next animation step
        output.text('' + Math.floor(Math.random() * 990)

        );
    }
}, 1000);

</script>
</head>
<body>
<div id="output">-</div>                    
</body>
</html>

动画随机数未显示(jscript 未运行,仅带有'-' characher/css 的框)

我的代码有什么问题?

【问题讨论】:

    标签: javascript jquery html


    【解决方案1】:

    您只需要确保您的代码在页面呈现后得到执行。尝试用以下方式包装它:

    ...
    <script>
    $( function () {
        var output, started, duration, desired;
        // Constants
        duration = 5000;
    
        ...
    
        }, 1000);
    });
    </script>
    ...
    

    你可以在这里找到更多信息jQuery.ready()

    【讨论】:

      【解决方案2】:

      您需要将与 DOM 交互的代码包装在 $(document).ready() 回调中:

      $(document).ready(function() {
          ...
      })
      

      【讨论】:

      【解决方案3】:

      你好,使用这个 javascript 代码就可以了

      <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
      <script>
      $(document).ready(function() {
      var output, started, duration, desired;
      // Constants
      duration = 5000;
      desired = '50';
      
      // Initial setup
      output = $('#output');
      started = new Date().getTime();
      
      // Animate!
      animationTimer = setInterval(function() {
          // If the value is what we want, stop animating
          // or if the duration has been exceeded, stop animating
          if (output.text().trim() === desired || new Date().getTime() - started > duration) {
              console.log('animating');
              // Generate a random string to use for the next animation step
              output.text('' + Math.floor(Math.random() * 990)
      
              );
      
          } else {
              console.log('animating');
              // Generate a random string to use for the next animation step
              output.text('' + Math.floor(Math.random() * 990)
      
              );
          }
      }, 1000);
      })
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-04-21
        • 1970-01-01
        相关资源
        最近更新 更多