简体   繁体   English

将变量整数传递给jquery函数?

[英]Pass a variable integer into jquery function?

Be so kind as to take a look at this jsfiddle: http://jsfiddle.net/ne6sg/2/ 非常友好地看看这个jsfiddle: http//jsfiddle.net/ne6sg/2/

    <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>

<script>
$(document).ready(function() {
setInterval(function(){
var interval = 500;
var paras = $('font');
var rand = Math.floor(Math.random() * paras.length);    
paras.eq(rand).addClass('red');
 },500);   
});
</script>
 <style>
.red {
    color: red;
}
 </style>
<font color=#040404>Frabjous Day</font>
<font color=#040404>Frabjous Day</font>
<font color=#040404>Frabjous Day</font>  
<font color=#040404>Frabjous Day</font>
<font color=#040404>Frabjous Day</font>
<font color=#040404>Frabjous Day</font>
<font color=#040404>Frabjous Day</font>
<font color=#040404>Frabjous Day</font>

Everything is working perfectly. 一切都很完美。 When run, my script gives the class .red to page elements at random, using the setInterval function to give the class at a set rate. 运行时,我的脚本会将类.red自动赋予页面元素,使用setInterval函数以设定的速率给出类。 This rate is defined by the number on line7 of the script. 此速率由脚本的第7行上的数字定义。 When this number is made larger, the speed at which the class is added is slowed down, because of the larger interval. 当这个数字变大时,由于间隔较大,增加类的速度会变慢。

However, I want that number on line7 to be an integer variable, and yet I can't get it to work. 但是,我希望第7行的数字是一个整数变量,但我无法让它工作。

IN CONCLUSION, why doesn't this work: http://jsfiddle.net/ne6sg/3/ ? 结论,为什么这不起作用: http//jsfiddle.net/ne6sg/3/

  <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>

<script>
$(document).ready(function() {
setInterval(function(){
var interval = 500;
var paras = $('font');
var rand = Math.floor(Math.random() * paras.length);    
paras.eq(rand).addClass('red');
 },(interval));   
});
</script>
 <style>
.red {
    color: red;
}
 </style>
<font color=#040404>Frabjous Day</font>
<font color=#040404>Frabjous Day</font>
<font color=#040404>Frabjous Day</font>  
<font color=#040404>Frabjous Day</font>
<font color=#040404>Frabjous Day</font>
<font color=#040404>Frabjous Day</font>
<font color=#040404>Frabjous Day</font>
<font color=#040404>Frabjous Day</font>

Thanks so much for your time. 非常感谢你的时间。

Define interval out of the scope of the setInterval 限定interval出的范围setInterval

var interval = 500; // this line, declare it here

setInterval(function(){

    var paras = $('font');
    var rand = Math.floor(Math.random() * paras.length);    
    paras.eq(rand).addClass('red');

}, interval); // so it exists in this context

You are defining the interval variable inside the function and trying to use it just outside. 您正在函数内部定义interval变量,并尝试在外部使用它。 Your js part would look something like the following: 你的js部分看起来像下面这样:

$(document).ready(function() {
    var interval = 500;
setInterval(function(){
    var paras = $('font');
    var rand = Math.floor(Math.random() * paras.length);    
    paras.eq(rand).addClass('red');
     },1);   
});

See this: http://jsfiddle.net/ne6sg/6/ 见: http//jsfiddle.net/ne6sg/6/

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

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