jquery - 使用 setInterval 分别显示 MySQL 表中的随机记录
问题描述
我希望每秒显示 5 个句子。当我在 RandomSentences 变量中列出每个句子时,以下代码有效:
(function() {
var timesRun = 0;
var runLimit = 5;
var RandomSentences = ['This is the first sentence', 'This is the second sentence', 'This is the third sentence', 'This is the fourth sentence', 'This is the fifth sentence'],
i = 0;
setInterval(function() {
timesRun += 1;
if (timesRun < runLimit) {
$('#changing-word').fadeOut(function() {
$(this).html(RandomSentences[i = (i + 1) % RandomSentences.length]).fadeIn();
});
}
}, 1000);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<span id="changing-word">Sentences go here</span>
但是,我有一张包含数千个句子的表格,我希望显示 5 个随机句子。我可以像这样选择这些记录:
SELECT sentences FROM table
ORDER BY RAND()
LIMIT 5
但是现在我如何将这 5 个随机记录放入 RandomSentences 变量中,以便我可以继续使用上面的 setInterval 每秒显示它们?
解决方案
推荐阅读
- javascript - 在现有项目中设置 Flow CLI Emulator,但每次运行时都会出现此服务器错误;flow emulator'/'flow project start-emulator'
- sql-server - 无法将 Glassfish 5.1.0 上的 Java 8 (291) Web 应用程序连接到 SQL Server 2014 实例
- javascript - 如何使用javascript删除自定义属性元素?
- python - 使用pypy时导入mnist模块出错
- reactjs - 2d 对象,其键与同一对象中值的键相同
- javascript - 使用计时器自动提交答案 (Javascript)
- java - 在 Trie 中设置 isLeaf 标志失败
- python - 如何使用 Numpy 通过来自另一个 DataFrame 的因子来缩放 DataFrame 中的列
- spring-webflux - 如何使用 sum 操作将 Mono 的 Stream 转换为 Flux 或将 Mono 的流减少为 Mono
- android - 未处理的承诺拒绝:未定义不是对象(评估 _expoLocation.requestForegroundPermissionsAsync)