Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/80.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何让相同的Javascript自调用函数模式并行运行多次而不覆盖值?_Javascript_Jquery_Oop - Fatal编程技术网

如何让相同的Javascript自调用函数模式并行运行多次而不覆盖值?

如何让相同的Javascript自调用函数模式并行运行多次而不覆盖值?,javascript,jquery,oop,Javascript,Jquery,Oop,我正在寻找一个OOP解决方案,使相同的对象文本/类并行或嵌套运行,而不干扰其他实例的变量 我在JSFIDLE中创建了一个演示,其中有一些按钮来显示所需的功能,我试图传递一个id来单独调用setTimeout操作,但我意识到所有变量也在不同的实例之间共享 (function( skillet, $, undefined ){ var t; var timer_is_on = 0; var startingPoint = 0; var endingPoint = 99; var

我正在寻找一个OOP解决方案,使相同的对象文本/类并行或嵌套运行,而不干扰其他实例的变量

我在JSFIDLE中创建了一个演示,其中有一些按钮来显示所需的功能,我试图传递一个id来单独调用setTimeout操作,但我意识到所有变量也在不同的实例之间共享

(function( skillet, $, undefined ){
  var t;
  var timer_is_on = 0;
  var startingPoint = 0;
  var endingPoint = 99;
  var timerLapse = 1000;
  var timerMatrixIndex = 0;
  var timerMatrix = [1000];
  var callback;

  function timedCount(timerLapse) {
    console.log(timerMatrix[timerMatrixIndex]);
    if (startingPoint > endingPoint || timer_is_on === 0) {
      clearTimeout(t[timerId]);
      skillet.stopCount();
    } else {
      document.getElementById("txt").value = startingPoint;
      if (timerMatrixIndex+1 < timerMatrix.length) {
        timerMatrixIndex++;
      }  
      startingPoint++;
      callback();
      t = { timerId: setTimeout( function() {timedCount(timerMatrix[timerMatrixIndex]);} ,timerLapse) }; 
    }
  }

  skillet.startCount = function(options) {
    options = options || {}; 
    startingPoint = options.startingPoint || 0; 
    endingPoint = options.endingPoint || 100;
    timerMatrix = options.timerMatrix || [1000];//[ 25,25,50,50,50,100,100,100,150,150,200,200,300,400,500,600,700,800,1000 ];
    timerId = options.timerId || 0;
    callback = options.callback || function(){};
    timerMatrixIndex= 0 ;

    if (!timer_is_on) {
      timer_is_on = 1;
      timedCount(timerMatrix[0]);
    }
  }

  skillet.stopCount = function() {
    clearTimeout(t[timerId]);
    timer_is_on = 0;
  }
}( window.skillet = window.skillet || {}, jQuery ))
(函数(煎锅,$,未定义){
变量t;
var定时器_为_on=0;
var起始点=0;
var端点=99;
var timerLapse=1000;
var timerMatrixIndex=0;
var timerMatrix=[1000];
var回调;
函数timedCount(timerLapse){
log(timerMatrix[timerMatrixIndex]);
如果(开始点>结束点| |计时器_开启===0){
clearTimeout(t[timerId]);
skillet.stopCount();
}否则{
document.getElementById(“txt”).value=startingPoint;
if(timerMatrixIndex+1
我正在寻找一种优雅的方式,不仅仅是将“myid”添加到所有方法或属性中,也许我应该创建一个类而不是一个对象文本,但我只是在学习Javascript OOP

有简单的方法吗

与示例中一样,“回调计数myid”应该独立于使用默认值的其他实例(不包括id)


现在,每次调用都会使上一次调用的参数为空

您的第一段语句有点矛盾。您要问的是如何使同一对象与其自身并行运行,而不是与其他实例冲突。如果有多个实例,则它不是同一个对象。如果变量的作用域是对象,它们就不会相互冲突。@Taplar谢谢,我试图尽可能最好地定义这个问题,我也不确定这是如何在内部执行的。你的第一段陈述有点矛盾。您要问的是如何使同一对象与其自身并行运行,而不是与其他实例冲突。如果有多个实例,则它不是同一个对象。如果变量的作用域是对象,它们就不会相互冲突。@Taplar谢谢,我试图尽可能最好地定义这个问题,我也不确定这是如何在内部执行的。