我有下面的代码,显示的简单js不起作用。我要怎么让js在蒙纳卡起作用?
这是我正在做的事情的代码。
顺便说一下,我在配置屏幕上添加了jQuery ()版本: 2.0.3。
<!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>发布于 2017-03-23 06:41:29
从我在代码中看到的情况来看,您在将time变量的值赋值给<div>元素时做了一个错误。这不是$("#countDown").txt(time);,而是$("#countDown").text(time);。
另外,从函数的名称-- countdown --我猜您正在尝试创建一个出现在屏幕上的计数器。在这种情况下,您不应该使用setTimeout(function, period)函数,因为它用于调用作为其参数传递的函数,只在句点(作为第二个参数传递)之后调用一次。因此,您应该使用setInterval(function(),period)函数,它在每次周期结束时都调用该函数。因此,您应该使用clearInterval()而不是clearTimeout()。
您的代码应该如下所示:
<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>https://stackoverflow.com/questions/42883926
复制相似问题