无法获取onclick事件以触发javascript中的my函数
我正在开发一个应用程序,它允许我单击网页上的按钮,然后启动计时器对象。但是,计时器不会在单击时启动无法获取onclick事件以触发javascript中的my函数,javascript,html,javascript-events,Javascript,Html,Javascript Events,我正在开发一个应用程序,它允许我单击网页上的按钮,然后启动计时器对象。但是,计时器不会在单击时启动 <script type="text/javascript"> var Timer = function() { // Property: Frequency of elapse event of the timer in millisecond this.Interval = 1000; // Property: Whether the
<script type="text/javascript">
var Timer = function()
{
// Property: Frequency of elapse event of the timer in millisecond
this.Interval = 1000;
// Property: Whether the timer is enable or not
this.Enable = new Boolean(false);
// Event: Timer tick
this.Tick;
// Member variable: Hold interval id of the timer
var timerId = 0;
// Member variable: Hold instance of this class
var thisObject;
// Function: Start the timer
this.Start = function()
{
this.Enable = new Boolean(true);
thisObject = this;
if (thisObject.Enable)
{
thisObject.timerId = setInterval(
function()
{
thisObject.Tick();
}, thisObject.Interval);
}
};
// Function: Stops the timer
this.Stop = function(){
thisObject.Enable = new Boolean(false);
clearInterval(thisObject.timerId);
};
};
//counts down the timer
function timer_tick()
{
index = index - 1;
document.getElementById("blue").innerHTML =index;
if (index === 0)
{
obj.Stop();
}
}
变量计时器=函数()
{
//属性:计时器的消逝事件频率(毫秒)
这个。间隔=1000;
//属性:计时器是否启用
this.Enable=新布尔值(false);
//事件:计时器滴答声
这个。勾选;
//成员变量:计时器的保持间隔id
var-timerId=0;
//成员变量:保持该类的实例
var这个对象;
//功能:启动定时器
this.Start=函数()
{
this.Enable=新布尔值(true);
thisObject=这个;
if(thisObject.Enable)
{
thisObject.timerId=setInterval(
函数()
{
thisObject.Tick();
},thisObject.Interval);
}
};
//功能:停止计时器
this.Stop=function(){
thisObject.Enable=新布尔值(false);
clearInterval(thisObject.timerId);
};
};
//倒计时
函数计时器_tick()
{
指数=指数-1;
document.getElementById(“蓝色”).innerHTML=索引;
如果(索引==0)
{
obj.Stop();
}
}
如果我删除函数startBlue(),但将代码留在其中,计时器将在页面加载时运行。因此,在看了我的代码之后,我认为问题在于我在哪里调用startBlue(在下面的html中)或者在这个函数中的某个地方
function startBlue(){
var index = 300;
var obj = new Timer();
obj.Interval = 1000;
obj.Tick = timer_tick();
obj.Start();
}
</script>
<body>
<div id ="Objectives">
Your Objectives:
<br>
<br>
<label>Blue Buff:</label>
<button id="yosb" onclick ="startBlue();">Start Blue</button>
<h2 id = "blue">
</h2>
</div>
</html>
函数startBlue(){
var指数=300;
var obj=新计时器();
目标间隔=1000;
obj.Tick=定时器_Tick();
obj.Start();
}
你的目标:
蓝色浅黄色:
发蓝
下一行
<button id="yosb" onclick ="startBlue();">Start Blue</button>
Start蓝色
应改为
<button id="yosb" onclick="startBlue();">Start Blue</button>
Start蓝色
您正在单击和=之间添加不必要的空格 你的问题不是空间,而是范围和参考问题
var index, obj; //define index and obj outside of startBlue().
function startBlue(){
index = 300; //initialize without redefining (remove 'var').
obj = new Timer(); //initialize without redefining (remove 'var').
obj.Interval = 1000;
obj.Tick = timer_tick; // store the actual function in obj.Tick, not the return (remove the '()' from the end of timer_tick)
obj.Start();
}
如果在函数内定义变量,则该变量只能在该函数的作用域内访问。通过在外部定义,您可以从外部范围访问该变量
要将函数存储在变量中,必须确保函数名末尾不包含paren。否则,将首先执行函数,然后返回值将存储在变量中,而不是存储在函数本身中。onclick=“startBlue();”应该是onclick=“startBlue();”(前面没有空格=)如果下面的答案对您有效,请选择它作为答案。如果不是的话,我很乐意再帮你一点。维维克瓦萨尼,这就是诀窍谢谢你!我修复了这一行,但在初始化onclick事件时仍然存在问题。计时器的用途是什么?