Don'我不明白为什么我的setInterval+jQuery;不起作用
Don't understand why my setInterval + jQuery doesn't work
总之,我正在尝试使用一个随机报价生成器。我的代码很简单。。。
var myQuotes = [
{
quote: "To err is human; to forgive, divine.",
cite: "Alexander Pope"},
{
quote: "Reports of my death have been greatly exaggerated.",
cite: "Mark Twain"}
];
var randomQuote = Math.floor(Math.random() * myQuotes.length);
$('.quote').html(myQuotes[randomQuote].quote); // #1
$('.cite').html(myQuotes[randomQuote].cite);
setInterval(function() {
$('.quote').fadeOut();
$('.quote').fadeIn().html(myQuotes[randomQuote].quote); // #2
}, 3000);
加载时,它显示#1很好,但#2似乎不起作用。。。它只是不断地闪烁着以前的那个,#1中的那个。我有什么不明白的?
您必须将randomQuote变量放入setInterval中,以便它更新:
setInterval(function() {
randomQuote = Math.floor(Math.random() * myQuotes.length);
$('.quote, .cite').fadeOut("slow", function() {
$('.quote').fadeIn("slow").html(myQuotes[randomQuote].quote);
$('.cite').fadeIn("slow").html(myQuotes[randomQuote].cite);
});
}, 3000);
http://jsfiddle.net/av1xg897/1/
相关文章:
- 为什么不't Javascript对我的输入值进行了一些重新检查
- 知道为什么我的旋转木马不会自动更改图片吗
- 为什么我的d3.jsselectAll+过滤器没有过滤
- 为什么不't我的变量在我的控制器中填充后在我的视图中呈现
- 为什么我的点击事件没有使用 react js 触发
- 为什么我的JavaScript堆栈排序函数不起作用
- 为什么使用immutableJS我的状态没有改变
- Don'我不明白为什么我的setInterval+jQuery;不起作用
- 如何在angular js中使用指令.不适用于我的情况,为什么
- 我的jquery代码不起作用.为什么?
- 为什么不't Express呈现我的React应用程序
- 为什么我的按钮组件提交它所在的表单
- 为什么不是'我的JS滑块正在工作
- 为什么不是'我的函数在解析云代码中工作吗?当我在Angular和Express中测试时,它是有效的
- 为什么不'当单元测试出现解析错误时,我的因果报应测试会失败
- 我不知道为什么我的代码是错误的?又有什么错
- 为什么我的While循环不起作用
- 为什么我的控制器没有启动函数.js脚本
- 为什么我的上下文选择器和.buttonset()在ie中花费了这么长时间
- 为什么我的函数没有被调用呢