我正在尝试创建一个切换按钮,当按下切换按钮时它应该运行一个功能。并且功能应该在切换关闭时停止。但是每当切换打开时,它应该不断运行功能。
JSBIN 试试:http: //jsbin.com/AbIzuNO/1/edit
HTML:
<input type="checkbox" id="check" />
<label for="check">Toggle</label>
JS:
var Autoreload;
$("#check").button({
icons: {
primary: "ui-icon-circle-triangle-s"
}
}).click(function () {
var iconClass;
if ($(this).is(':checked')) {
iconClass = "ui-icon-circle-triangle-e";
Autoreload = setTimeout(function () {
//Run somefunction here constantly untill cleared
//$.getJSON( "ajax/test.json", function( data ) {
// some success calls
//});
console.log("Running");
}, 100);
} else {
iconClass = "ui-icon-circle-triangle-s";
clearTimeout(Autoreload);
}
$(this).button("option", {
icons: {
primary: iconClass
}
});
});