我用这个 javascript 框架创建了一个演示文稿http://lab.hakim.se/reveal-js/#/ http://lab.hakim.se/reveal-js/#/当某些幻灯片出现时,我想执行函数 stats(),它每 5 秒显示来自 API 的一些数据,但仅当该幻灯片出现时
function stats(){
$.ajax({
type: "GET",
url: url_survey,
dataType: "json",
success :
function (data) {
//some code to display data
},
error:
//some code
});
}
在 HTML 文件中我有这个:
<section>
<div id="stats">
</div>
</section>
我该怎么做?我写了这段代码,但它总是有效,不仅是在幻灯片出现时
function check(){
if($("#stats").is(":visible"))
stats();
}
check();
setInterval(function(){check()}, 2000);
这可以通过使用 Reveal.js 状态来实现(https://github.com/hakimel/reveal.js#states https://github.com/hakimel/reveal.js#states).
1) 向应触发您的方法的部分添加一个独特的状态。
<section data-state="stats">
<h2>This will trigger a state event with the name "stats".</h2>
</section>
2) 使用reveal.js API 将侦听器添加到您的自定义状态。
Reveal.addEventListener( 'stats', function() {
// Called each time the slide with the "stats" state is made visible
} );
完整的工作示例:https://gist.github.com/hakimel/cea4305a33713d4a5a7e https://gist.github.com/hakimel/cea4305a33713d4a5a7e
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)