简体   繁体   English

我的标题的切换颜色在红色和绿色之间

[英]Toggeling Color of my header between red and green

I have wrote a code, its a simple code that changes the color of HTML Heading to red and then to green and it keeps going on. 我已经编写了一个代码,它是一个简单的代码,它将HTML标题的颜色更改为红色,然后变为绿色,并且一直在运行。 But my question here is regarding defining the variable "value". 但是我的问题是关于定义变量“值”。 So every-time i define the variable value globally the loop for toggling color between red and green works, but when i declare it inside my start function the loop doesn't work it just get stuck in green (ie my first instance). 因此,每次我在全局范围内定义变量值时,都会在红色和绿色之间切换颜色,但是当我在启动函数中声明它时,该循环不起作用,它只会卡在绿色中(即我的第一个实例)。 Any suggestions why it behaves like this?? 任何建议,为什么它会这样?

Regards, New Coder. 问候,新编码员。

// first i decleared header value

header = document.querySelector('h1')


// after that i needed a toggel function to toggel the boolean values


function toggel(input)

{

output=!(input)

return output

}

// here if i comment var value out and put this inside the function start 
it will just work for one instance and code will stop. And if put it outside like its down below, the LOOP will keep on going long toggling red to green and back to red

var value = toggel(true)


//  after that i made a fucntion called "start" which will be called in 
1000 miliseconds of interval and every time its called i toggel the value 
of boolean value

function start()

{

function colorchanger()

{

  // var value = toggel(value)

  if (value == true)

  {

    return "red";

  }

  else

  {

    return "green";

      }

}

 out = colorchanger()

 console.log(out)

 header.style.color = out;

 value = toggel(value)

}

setInterval("start()", 1000);

so compliqued ? 如此恭维?

 header = document.querySelector('h1'); setInterval(() => header.classList.toggle('onGreen') , 1000); 
  h1 { color:red; } h1.onGreen { color:green; } 
 <h1>H1 "Header"</h1> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM