首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Monaca Javascript无效

Monaca Javascript无效
EN

Stack Overflow用户
提问于 2017-03-19 07:10:59
回答 1查看 116关注 0票数 0

我有下面的代码,显示的简单js不起作用。我要怎么让js在蒙纳卡起作用?

这是我正在做的事情的代码。

顺便说一下,我在配置屏幕上添加了jQuery ()版本: 2.0.3。

代码语言:javascript
复制
<!DOCTYPE HTML>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
  <meta http-equiv="Content-Security-Policy" content="default-src * data:; style-src * 'unsafe-inline'; script-src * 'unsafe-inline' 'unsafe-eval'">
  <script src="components/loader.js"></script>
  <script src="lib/onsenui/js/onsenui.min.js"></script>

  <link rel="stylesheet" href="components/loader.css">
  <link rel="stylesheet" href="lib/onsenui/css/onsenui.css">
  <link rel="stylesheet" href="lib/onsenui/css/onsen-css-components.css">
  <link rel="stylesheet" href="css/style.css">

  <script>
    //this starts the count down. 
    //time should be in seconds
    function countDown(time){
        $("#countDown").txt(time);
        var timeout=setTimeout(function(){
            time--;
            $("#countDown").txt(time);
            if(time==0) clearTimeout(timeout);
        }, 1000);
    }

    ons.ready(function() {
        countDown(10);
    });

  </script>
</head>
<body>
    <div id="countDown"></div>
</body>
</html>
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-03-23 06:41:29

从我在代码中看到的情况来看,您在将time变量的值赋值给<div>元素时做了一个错误。这不是$("#countDown").txt(time);,而是$("#countDown").text(time);

另外,从函数的名称-- countdown --我猜您正在尝试创建一个出现在屏幕上的计数器。在这种情况下,您不应该使用setTimeout(function, period)函数,因为它用于调用作为其参数传递的函数,只在句点(作为第二个参数传递)之后调用一次。因此,您应该使用setInterval(function(),period)函数,它在每次周期结束时都调用该函数。因此,您应该使用clearInterval()而不是clearTimeout()

您的代码应该如下所示:

代码语言:javascript
复制
<script>
    //this starts the count down. 
    //time should be in seconds
    function countDown(time){
        $("#countDown").text(time);
        var timeout=setInterval(function(){
            time--;
            $("#countDown").text(time);
            if(time==0) clearInterval(timeout);
        }, 1000);
    }

    ons.ready(function() {
        countDown(10);
    });
</script>
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42883926

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档