সেট ইনটারভাল বন্ধ করুন


120

আমি errorহ্যান্ডলারের এই অন্তরকে বারবার চালানো থেকে থামাতে চাই । এটি কি সম্ভব, এবং যদি তা হয় তবে কীভাবে?

// example code
$(document).on('ready',function(){
    setInterval(updateDiv,3000);
});

function updateDiv(){
    $.ajax({
        url: 'getContent.php',
        success: function(data){
            $('.square').html(data);
        },
        error: function(){
            $.playSound('oneday.wav');
            $('.square').html('<span style="color:red">Connection problems</span>');
            // I want to stop it here
        }
    });
}

উত্তর:


242

setIntervalক্লিক হ্যান্ডলারের ক্ষেত্রের মধ্যে আপনাকে একটি পরিবর্তনশীলটির রিটার্ন মান সেট করতে হবে , তারপরে এটি ব্যবহার করুন clearInterval():

var interval = null;
$(document).on('ready',function(){
    interval = setInterval(updateDiv,3000);
});

function updateDiv(){
    $.ajax({
        url: 'getContent.php',
        success: function(data){
            $('.square').html(data);
        },
        error: function(){
            clearInterval(interval); // stop the interval
            $.playSound('oneday.wav');
            $('.square').html('<span style="color:red">Connection problems</span>');
        }
    });
}


আপনি setTimout()যা কেবল একবারে রান করতে পারেন তা ব্যবহার করতে পারেন
— জাস্টিন লিউ

21

clearIntervalএটি পরিবর্তন করতে একটি ভেরিয়েবল ব্যবহার করুন এবং কল করুন ।

var interval;

$(document).on('ready',function()
  interval = setInterval(updateDiv,3000);
  });

  function updateDiv(){
    $.ajax({
      url: 'getContent.php',
      success: function(data){
        $('.square').html(data);
      },
      error: function(){
        $.playSound('oneday.wav');
        $('.square').html('<span style="color:red">Connection problems</span>');
        // I want to stop it here
        clearInterval(interval);
      }
    });
  }

11

setIntervalআপনাকে একটি ভেরিয়েবলের জন্য ফাংশনের ফেরত মান নির্ধারণ করতে হবে

var interval;
$(document).on('ready',function(){
    interval = setInterval(updateDiv,3000);
});

এবং তারপরে clearInterval(interval)এটি পরিষ্কার করতে ব্যবহার করুন ।


8

এটি ব্যবহার করুন আশা করি আপনাকে সাহায্য করবে

var interval;

function updateDiv(){
    $.ajax({
        url: 'getContent.php',
        success: function(data){
            $('.square').html(data);
        },
        error: function(){
            /* clearInterval(interval); */
            stopinterval(); // stop the interval
            $.playSound('oneday.wav');
            $('.square').html('<span style="color:red">Connection problems</span>');
        }
    });
}

function playinterval(){
  updateDiv(); 
  interval = setInterval(function(){updateDiv();},3000); 
  return false;
}

function stopinterval(){
  clearInterval(interval); 
  return false;
}

$(document)
.on('ready',playinterval)
.on({click:playinterval},"#playinterval")
.on({click:stopinterval},"#stopinterval");

4

আমরা স্পষ্ট বিরতি কল করে সেট ব্যবধানটি সহজেই থামাতে পারি

var count = 0 , i = 5;
var vary = function intervalFunc() {
  count++;
      console.log(count);
    console.log('hello boy');  
    if (count == 10) {
      clearInterval(this);
    }
}

  setInterval(vary, 1500);

খুব খারাপ অনুশীলন। 1 ম এর অর্থ এই হতে পারে যে এটি setIntervalঅনির্দিষ্টকালের জন্য ফাঁস হতে পারে - এটি নিয়ন্ত্রণ করতে কোনও ভের যুক্ত নেই। 2 য় সময় আপনি পরিষ্কার করেন নি this, যেহেতু thisসময় বিরতি নয়। আপনি যদি ব্যবহার করেন তবে আপনি TypeScriptসমস্যায় পড়ে যাবেন:No overload matches this call.Overload 1 of 2, '(intervalId: Timeout): void', gave the following error: Argument of type 'this' is not assignable to parameter of type 'Timeout'.
— পেড্রো ফেরেরিরা

-2

var flasher_icon = function (obj) {
    var classToToggle = obj.classToToggle;
    var elem = obj.targetElem;
    var oneTime = obj.speed;
    var halfFlash = oneTime / 2;
    var totalTime = obj.flashingTimes * oneTime;

    var interval = setInterval(function(){
        elem.addClass(classToToggle);
        setTimeout(function() {
            elem.removeClass(classToToggle);
        }, halfFlash);
    }, oneTime);

    setTimeout(function() {
        clearInterval(interval);
    }, totalTime);
};

flasher_icon({
    targetElem: $('#icon-step-1-v1'),
    flashingTimes: 3,
    classToToggle: 'flasher_icon',
    speed: 500
});
.steps-icon{
    background: #d8d8d8;
    color: #000;
    font-size: 55px;
    padding: 15px;
    border-radius: 50%;
    margin: 5px;
    cursor: pointer;
}
.flasher_icon{
  color: #fff;
  background: #820000 !important;
  padding-bottom: 15px !important;
  padding-top: 15px !important;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet"> 

<i class="steps-icon material-icons active" id="icon-step-1-v1" title="" data-toggle="tooltip" data-placement="bottom" data-original-title="Origin Airport">alarm</i>


4
এই কোডটি কী করে এবং কীভাবে এটি প্রশ্নের সাথে সম্পর্কিত তা দয়া করে ব্যাখ্যা করুন।
— জেজেজে
আমাদের সাইট ব্যবহার করে, আপনি স্বীকার করেছেন যে আপনি আমাদের কুকি নীতি এবং গোপনীয়তা নীতিটি পড়েছেন এবং বুঝতে পেরেছেন ।
Licensed under cc by-sa 3.0 with attribution required.